Question

I am trying to understand how to link C code with SWI-Prolog, but I am having some difficulties. When I compile the following code and load it it works perfectly on OS X, but when I move the exact same code over to Ubuntu 12.04 it doesn't work. I get the error

ERROR: Exported procedure choose:choose/3 is not defined

And, of course. I can no longer call choose. When I tried to install this on a completely new virtual machine it did work for SWI-Prolog 5.10.4, but when I upgraded to the latest stable version it started failing again. If I use the old .so file from when I compiled it using 5.10.4 it still works. I am however unable to compile and link the new .so file with the new version of SWI-Prolog using swipl-ld.

What am I doing wrong?


binomial.pl

:- module(binomial, [choose/3]).
:- use_foreign_library(choose).

choose.c

#include <gmp.h>
#include <SWI-Prolog.h>

static foreign_t
pl_choose(term_t size, term_t take, term_t result)
{
  mpz_t rop;
  mpz_t n;
  unsigned long int k;
  int rc;

  mpz_init(rop);
  mpz_init(n);

  if ( PL_get_mpz(take, rop) && PL_get_mpz(size, n) )
    {
      k = mpz_get_ui(rop);
      mpz_bin_ui(rop, n, k);
      rc = PL_unify_mpz(result, rop);
    }
  else
    {
      rc = FALSE;
    }

  mpz_clear(rop);
  mpz_clear(n);
  return rc;
}

install_t
install_choose()
{
  PL_register_foreign("choose", 3, pl_choose, 0);
}

I build and link it by calling

swipl-ld -lgmp -shared -o choose choose.c
Was it helpful?

Solution

The problems is caused by the fact that the shared libswipl.so is installed in /usr/lib and the static libswipl.a in /usr/lib/swi-prolog/lib/arch, which causes the linker to pick the static version. The temporary work-around is to remove libswipl.a from /usr/lib/swi-prolog/lib/.

Future versions will install libswipl.a in /usr/lib, so you can also move the library although this will probably result in a conflict reported by apt if you upgrade to the next version.

OTHER TIPS

Smells a lot like this..

http://swi-prolog.996271.n3.nabble.com/Undefined-procedure-on-foreign-library-in-6-6-4-td14676.html

Suggest updating your question, adding the version of linux.. might be useful for the guys at swi.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top