next up previous
Next: Example of Using Up: The Foreign Include Previous: Foreign Code Hooks

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 gif.

The file pl-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_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 initialisation files, runs the at_initialisation/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.

int
PL_toplevel()
  Runs the goal of the -t toplevel switch (default prolog/0) and returns 1 if successfull, 0 otherwise.
void
PL_halt( int status)
  Cleanup the Prolog environment and calls exit() with the status argument.



Passani Luca
Tue Nov 14 08:58:33 MET 1995