Next: , Previous: Init and Deinit Functions, Up: Calling C from Prolog

6.2.7 Creating the Linked Foreign Resource

Suppose we have a Prolog source file containing:

foreign(f1, p1(+integer,[-integer])). foreign(f2, p2(+integer,[-integer])). foreign_resource(ex, [f1,f2]). :- load_foreign_resource(ex).

and a C source file ex.c with definitions of the functions f1 and f2, both returning SP_integer and having an SP_integer as the only parameter. The conversion declarations in state that these functions form the foreign resource ex. Normally, the C source file should contain the following two line near the beginning (modulo the resource name):

     #include <sicstus/sicstus.h>
     /* ex_glue.h is generated by splfr from the foreign/[2,3] facts.
        Always include the glue header in your foreign resource code.
     #include "ex_glue.h"

To create the linked foreign resource, simply type (to the Shell):

     % splfr ex.c

The linked foreign resource (file suffix .so is system dependent) has been created. It will be dynamically linked by the directive :- load_foreign_resource(ex). when the file is loaded. For a full example, see Foreign Code Examples.

Dynamic linking of foreign resources can also be used by runtime systems.

Send feedback on this subject.