[Up] [Contents] [Index] [Summary]

5.6.16 Embedding SWI-Prolog in a C-program

As of version 2.1.0, SWI-Prolog may be embedded in a C-program. To reach at a compiled C-program with SWI-Prolog as an embedded application is very similar to creating a statically linked SWI-Prolog executable as described in section 5.4.1.

The file ... /pl/include/stub.c defines SWI-Prologs default main program:

int main(int argc, char **argv, char **env) { if ( !PL_initialise(argc, argv, env) ) PL_halt(1); PL_install_readline(); /* delete if you don't want readline */ PL_halt(PL_toplevel() ? 0 : 1); }

This may be replaced with your own main C-program. The interface function PL_initialise() must be called before any of the other SWI-Prolog foreign language functions described in this chapter. PL_initialise() interprets all the command-line arguments, except for the -t toplevel flag that is interpreted by PL_toplevel().

int PL_initialise(int argc, char **argv, char **environ)
Initialises the SWI-Prolog heap and stacks, restores the boot QLF file, loads the system and personal initialisation files, runs the at_initialization/1 hooks and finally runs the -g goal hook.

PL_initialise() returns 1 if all initialisation succeeded and 0 otherwise. Various fatal errors may cause PL_initialise to call PL_halt(1), preventing it from returning at all.

void PL_install_readline()
Installs the GNU-readline line-editor. Embedded applications that do not use the Prolog toplevel should normally delete this line, shrinking the Prolog kernel significantly.

int PL_toplevel()
Runs the goal of the -t toplevel switch (default prolog/0) and returns 1 if successful, 0 otherwise.

void PL_halt(int status)
Cleanup the Prolog environment and calls exit() with the status argument.