Home Contents Index Summary Previous Next

5.2 Linking Foreign Modules

Foreign modules may be linked to Prolog in three ways. Using static linking, the extensions, a small description file and the basic SWI-Prolog object file are linked together to form a new executable. Using dynamic linking, the extensions are linked to a shared library (.so file on most Unix systems) or dynamic-link library (.DLL file on Microsoft platforms) and loaded into the the running Prolog process. (32).

5.2.1 What linking is provided?

The static linking schema can be used on all versions of SWI-Prolog. Whether or not dynamic linking is supported can be deduced from the prolog-flag open_shared_object (see current_prolog_flag/2). If this prolog-flag yields true, open_shared_object/2 and related predicates are defined. See section 5.4 for a suitable high-level interface to these predicates.

5.2.2 What kind of loading should I be using?

All described approaches have their advantages and disadvantages. Static linking is portable and allows for debugging on all platforms. It is relatively cumbersome and the libraries you need to pass to the linker may vary from system to system, though the utility program plld described in section 5.7 often hides these problems from the user.

Loading shared objects (DLL files on Windows) provides sharing and protection and is generally the best choice. If a saved-state is created using qsave_program/[1,2], an initialization/1 directive may be used to load the appropriate library at startup.

Note that the definition of the foreign predicates is the same, regardless of the linking type used.