Next: , Previous: , Up: Calling C from Prolog   [Contents][Index]


6.2.7 Creating the Linked Foreign Resource

Suppose we have a Prolog source file ex.pl containing:

% ex.pl
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 ex.pl 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.pl ex.c

The linked foreign resource ex.so (file suffix .so is system dependent) has been created. It will be dynamically linked by the directive :- load_foreign_resource(ex). when the file ex.pl 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.