6 Mixing C/C++ and Prolog

SICStus Prolog provides a bi-directional, procedural interface for program parts written in C and Prolog. The C side of the interface defines a number of functions and macros for various operations. On the Prolog side, you have to supply declarations specifying the names and argument/value types of C functions being called as predicates. These declarations are used by the predicate load_foreign_resource/1, which performs the actual binding of C functions to predicates.

In most cases, the argument/value type declarations suffice for making the necessary conversions of data automatically as they are passed between C and Prolog. However, it is possible to declare the type of an argument to be a Prolog term, in which case the receiving function will see it as a “handle” object, called an SP_term_ref, for which access functions are provided.

The C support routines are available in a development system as well as in runtime systems. The support routines include:

In addition to the interface described in this chapter, library(structs) and library(objects) (see lib-structs and lib-objects) allow Prolog to hold pointers to C data structures and arrays and access and store into fields in those data structures in a very efficient way, allowing the programmer to stay completely inside Prolog.


Send feedback on this subject.