next up previous
Next: Dynamic Linking based Up: Linking Foreign Modules Previous: What kind of

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.

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_halt(PL_toplevel() ? 0 : 1);
}

Now, a new executable may be created using the make(1) Makefile fragment below. The correct versions for the variables may be obtained using the feature/2 predicate:

?- feature(c_cc,         CC).
?- feature(c_ldflags,    LDFLAGS).
?- feature(c_staticlibs, STATICLIBS).
?- feature(c_libs,	 LIBS).
?- feature(arch,	 ARCH).
?- feature(home,	 HOME).

CC=gcc
HOME=/usr/local/lib/pl-2.0.6
CFLAGS=-I$(HOME)/include
LDFLAGS=
STATICLIBS=/usr/lib/libc.a
LIBS=-lreadline -ltermcap -lm
ARCH=i486-linux

PLOBJ=$(HOME)/$(ARCH)/pl.o
OBJ=lowercase.o stub.o

lwpl:	$(PLOBJ) $(OBJ)
	$(CC) -o $@ $(LDFLAGS) $(PLOBJ) $(OBJ) $(STATICLIBS) $(LIBS)

The resulting lwpl file will load the standard prolog bootstrap file. Normally, your extensions will partly be written in Prolog. In this case you may wish to create a new bootfile that includes the standard Prolog system and your extensions. Write a toplevel loadfile that loads your application and add the following to your Makefile:

PLFILES:	load.pl foo.pl bar.pl

lwpl.qlf:	lwpl $(PLFILES)
		./lwpl -g mytoplevel \
		       -b $(HOME)/boot/init.pl \
		       -c load.pl

See also section gif for compiling Prolog sources using the -c option.



Passani Luca
Tue Nov 14 08:58:33 MET 1995