[Up] [Contents] [Index] [Summary]

5.4.1 Static Linking

Below is an outline of the files structure required for statically linking SWI-Prolog with foreign extensions. ... /pl refers to the SWI-Prolog home directory (see feature/2). <arch> refers to the architecture identifier that may be obtained using feature/2.

... /pl/runtime/<arch>/libpl.aSWI-Library
... /pl/include/SWI-Prolog.hInclude file
... /pl/include/SWI-Stream.hStream I/O include file
... /pl/include/SWI-ExportsExport declarations (AIX only)
... /pl/include/stub.cExtension stub

The definition of the foreign predicates is the same as for dynamic linking. Unlike with dynamic linking however, there is no initialisation function. Instead, the file ... /pl/include/stub.c may be copied to your project and modified to define the foreign extensions. Below is stub.c, modified to link the lowercase example described later in this chapter:

/* Copyright (c) 1991 Jan Wielemaker. All rights reserved. jan@swi.psy.uva.nl Purpose: Skeleton for extensions */ #include <stdio.h> #include <SWI-Prolog.h> extern foreign_t pl_lowercase(term, term); PL_extension PL_extensions [] = { /*{ "name", arity, function, PL_FA_<flags> },*/ { "lowercase", 2 pl_lowercase, 0 }, { NULL, 0, NULL, 0 } /* terminating line */ }; int main(int argc, char **argv, char **env) { if ( !PL_initialise(argc, argv, env) ) PL_halt(1); PL_install_readline(); /* delete if not required */ PL_halt(PL_toplevel() ? 0 : 1); }

Now, a new executable may be created by compiling this file and linking it to libpl.a from the runtime directory and the libraries required by both the extensions and the SWI-Prolog kernel. This may be done by hand, or using the plld utility described in secrefplld.