Using Shared Object Files and Archive Files

By default, foreign code must be packaged as shared object files for use with load_foreign_executable/1. Archive files are used for statically linking foreign code to executables (see sap-srs).

A shared object file is constructed from a list of object files (and libraries) using the system linker. An archive file is constructed from a list of object files using special tools. In both cases, the object files are generated using the foreign language compiler.

Linkers require special options to construct a shared object file, and may require that the object files used to generate the shared object files be compiled with position independent code or other special compiler options. Under Windows, Quintus Prolog also requires a special compiler option for inclusion into archive files.

Let CC denote the compiler command, let SFLAGS denote the compiler options for shared object files, let AFLAGS denote the compiler options for archive files, and let LFLAGS denote the linker options. The following table gives these options for the supported Quintus Prolog platforms.

Platform CC AFLAGS SFLAGS LFLAGS

linux gcc (none) -fPIC -shared

alpha cc (none) (none) -taso -shared -expect_unresolved 'Q?_*'

hppa cc +DAportable +Z +DAportable -b

hppa gcc (none) -fPIC -shared

sgi cc -n32 -n32 -n32 -shared

sgi gcc -mabi=n32 -mabi=n32 -fPIC -mabi=n32 -shared

sun4-5 cc (none) -K pic -G

sun4-5 gcc (none) -fPIC -shared

rs6000 cc (none) (none) -bI:runtime-directory/prolog.exp -e QP_entry glue.o

Windows cl /MD /MD /dll

To build a shared object file, say mylib.so, under UNIX, issue the following:

     % CC SFLAGS -c SOURCE1
     % ...
     % CC SFLAGS -c SOURCEn
     % ld -o mylib.so LFLAGS OBJECTS
     

When you build a shared object file, say mylib.dll, under Windows, a corresponding import library will normally also be built. Consult the Windows documentation for details. Issue the following:

     C:\> cl SFLAGS /c SOURCE1
     C:\> ...
     C:\> cl SFLAGS /c SOURCEn
     C:\> link /dll /out:mylib.dll OBJECTS
     

To build a archive file, say mylib.a, under UNIX, issue the following:

     % CC AFLAGS -c SOURCE1
     % ...
     % CC AFLAGS -c SOURCEn
     % ar r mylib.a OBJECTS
     % ranlib mylib.a
     

For archive files under Windows, a special naming convention is used: an extra s is placed before the .lib extension, to distinguish archive files from import libraries (see sap-rge-sos). To build an archive file, say mylibs.lib, issue the following:

     C:\> cl AFLAGS /c SOURCE1
     C:\> ...
     C:\> cl AFLAGS /c SOURCEn
     C:\> link /lib /out:mylibs.lib OBJECTS
     

Platform specific notes:


alpha
Foreign code must be compiled in native mode, i.e. not using the -xtaso_short option.
rs6000
You must supply the file glue.o. See QuintusDir/generic/qplib3.5/structs/library/rs6000/structs_lnk.c for an example.
Windows
If building a DLL that calls any of the Quintus Prolog C API functions exported from the Runtime Kernel DLL (qpeng.dll) or the Embedding Layer DLL (libqp.dll) then you must also link in the import libraries for these DLLs, which are named qpeng.lib and libqp.lib and reside in the directory quintus-directory\lib\ix86. This is needed because DLLs must have their external references resolved at link time rather than at load time, in contrast to typical UNIX shared library implementations. Run the qpvars.bat file to set-up the environment variables necessary for the C-compiler and linker to fine the needed Quintus files.

The makefile.win file in the Quintus Library directory quintus-directory\src\library contains an example of how to build a DLL for dynamic loading into Quintus Prolog.

The following two sections describe the use of shared object files in Quintus Prolog: