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