Calling Prolog from C

I’m trying to call prolog from C. Consider this simple program:

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

int main() {        
    static predicate_t pred;
    term_t result = PL_new_term_ref();
    printf("test\n");
}

and compiled with
gcc -I /usr/local/lib/swipl/include/ -L /usr/local/lib/swipl/bin/x86_64-linux/swipl main.c -shared -fPIC
The compilation succeed however, when i run the program, i get a segmentation fault. I’ve just updated SWI by pulling from the main repository on GitHub. What i’m i doing wrong?

Thanks

Don’t you have to initialise the Prolog engine first?

1 Like

I don’t know. According to the example at the following link https://www.swi-prolog.org/pldoc/man?section=calling-prolog-from-c there are no other functions i should call

It’s just guesswork here, but I think you need to initialise the Prolog engine, when the main() is on the C side.

Yip. See Embedding SWI-Prolog in other applications