I’m using: SWI-Prolog version 9.3.18 for x64-win64
I want the code to: convert a prolog integer term to a C integer using PL_get_integer
But what I’m getting is: false, even though I confirm the prolog term is indeed integer
My C code in simple_prolog_c_test.c
looks like this:
#include <SWI-Prolog.h>
#include <stdbool.h>
#include <stdio.h>
static foreign_t pl_mytest(term_t vector3_pl) {
term_t x_pl = PL_new_term_ref();
int x_c;
if (!PL_get_arg(1, vector3_pl, x_pl))
printf("could not extract x_pl\n");
else printf("extracted x_pl\n");
printf("x_pl is int? %d\n", PL_is_integer(x_pl));
if (!PL_get_integer(x_pl, &x_c))
printf("value of x is %d\n", x_c);
else printf("could not get x_pl into x_c\n");
return true;
}
install_t
install_simple_prolog_c_test(void)
{
PL_register_foreign("mytest", 1, pl_mytest, 0);
}
which I compile to a .dll with
PS C:\Users\***> & 'C:\Program Files\swipl\bin\swipl-ld.exe' -shared -o simple_prolog_c_test.dll simple_prolog_c_test.c
My prolog interface to the C code is defined in simple_prolog_c_test.pro
: [EDIT: sorry, I had the wrong file here before, thanks Peter for picking up on this.]
:- module(simple_prolog_c_test, [
mytest/1
]).
:- use_foreign_library(foreign(simple_prolog_c_test)).
And then my swipl session showing PL_get_integer returns false is this:
PS C:\Users\***> swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.3.18)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
1 ?- ['simple_prolog_c_test.pro'].
true.
2 ?- mytest(myvector(1, 2, 3)).
extracted x_pl
x_pl is int? 1
could not get x_pl into x_c
true.
3 ?- halt.
PS C:\Users\***>
Thanks for your attention!