Foreign predicates must always appear in modules, and these modules can contain only foreign predicates. A foreign module differs from a Prolog module in that the foreign module's source file must appear in a *.c file rather than a *.P file (or .pl file). This *.c file cannot contain a main() function. Furthermore, a *.P file with the same name must not be present or else the *.c file is ignored and the module is compiled as a regular Prolog module. The interface part of a foreign module, which has the same syntax as that of a normal module, is written in Prolog and must appear in a *.H file. If the lower-level interface is used, this *.H file contains explicit export/1 declarations for the the foreign predicates that are to be used by other modules; if the higher-level interface is used, the declarations have the form foreign_pred/1.
The Prolog predicates attached to foreign functions are deterministic, in the sense that they succeed at most once for a given call and are not re-entered on backtracking. Note that this requirement imposes no serious limitation, since it is always possible to divide a foreign predicate into the part to be done on the first call and the part to be redone on backtracking. Backtracking can then take place at the Prolog level where it is more naturally expressed.
A foreign module can be compiled or consulted just like a normal Prolog module. Currently, predicates consult/[1,2] recompile both the *.c and the *.H files of a foreign module when at least one of them has been changed from the time the corresponding object files have been created (see the section Compiling and Consulting in Volume 1) 2.1. The C compiler used to compile the *.c files can be set as a defaults to that used for the configuration of XSB (refer to the section Getting Started with XSB in Volume 1). This default behavior includes the C compilation options used to compile XSB when it was configured, along with a default set of include files so that header files in XSB directories can be obtained. Alternately, the user can add options to be passed to the C compiler. To give an example, the following command will compile file file.c using the default C Compiler with optimization and by including /usr/local/X11/R6/include to the directories that will be searched for header files.
Prolog-specific directives such as index, hilog, table, auto_table or even import make no sense in the case of a foreign module and thus are ignored by the compiler. However, another directive, namely ldoption, is recognized in a foreign module and is used to instruct the dynamic loading and linking of the module. The syntax of the ldoption directive is simply: