C programming environment for creaing a foreign "c" predicate

Hello,

I have difficulties getting the c development environment and workflow going for creating a simple foreign predicate. On ubuntu 18.01 I tried to use eclipse c/c++ but stumbled in getting the build to work (for a sample main.c program).

I now use CLion trial, and can build – and could get the sample build (http://www.swi-prolog.org/pldoc/man?section=foreignlink).

I now have a so file in the debug folder and tried to run,

swipl-ld -o libelem.so …/library.c

and then gget "undefined reference to ‘main’

I haven’t programmed in c for ages and doing my first steps again with everything new to me.

Any help that fills the blanks in the documentation, that gets me to complile and load a foreign predicate, that receives an atom, would be much appreciated

thank you,

Dan

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

static foreign_t
pl_say_hello(term_t to)
{ char *a;

if ( PL_get_atom_chars(to, &a) )
{ printf(*a);

PL_succeed;
}

PL_fail;
}

install_t
install_mylib()
{ PL_register_foreign(“say_hello”, 1, pl_say_hello, 0);
}

I wish there was a step by step tutorial from:
1 setting up the development environment,
2. adding the right include paths into the right configuration files
3. to example code, for reading and returning atoms, to building,
4. to building the files
4. to ensuring the shared library is then placed in the right folders to be found by prolog
5. to the pl file that includes the foreign lib
6. to its use

as well as how to debug in such a setup

Dan

I now tried to copy the built so file into /usr/lib/swi-prolog/lib/x86_64-linux

then tried to use it via:

:-use_foreign_library(foreign(libelem_lib)).

but that gave me a compile error:

demo_5.pl:2: Initialization goal raised exception:
No install function in ‘/usr/lib/swi-prolog/lib/x86_64-linux/libelements_lib.so’
Tried: [install_libelements_lib,install]
In:

I now noticed the example

http://www.swi-prolog.org/pldoc/man?section=foreignxmp

I will try to get it to work … first, in the command line, as described, and then i CLion

Dan

Great!

The example worked for me on the command line; after tweaking the include path to the one I have on ubuntu …

Now i need to figure out how to get this to work in CLion – and then how to place the generated *.so file into a folder where it can be found

I can then finally start working through the foreign interface api to develop my little linked list predciates.

Dan