How Embedding Works

The next three sections describe the major areas of Prolog that can be redefined to facilitate the embedding of Prolog code in foreign language applications:

Consider the details presented in these sections in the context of this overview of the process of creating a program with a Prolog component:

  1. Write the foreign code (prog.c) and the Prolog code (, using the steps for the C calling Prolog interface described in fli-ffp-bas-sum to call the Prolog predicates. Most likely you will be defining a main() routine; in this case be sure to call QP_initialize(). An example is found in the reference page.
  2. Determine whether it is necessary to customize any of the API modules for initialization, memory management or input/output. This is discussed in fli-emb-how-mai, fli-emb-how-mem, and fli-emb-how-iou.
  3. If customization is required, and it seldom will be, rewrite the relevant modules.
  4. Produce the executable:
    1. Compile using qpc -c
    2. Compile prog.c using e.g. cc -c prog.c
    3. Link the two using qld. A typical call would be:
                     % qld -Dd component.qof prog.o -o BigApplication

      You may also want to link in QUI in order to be able to use the debugger, as described in dbg-sld. The process of linking QUI into an application is discussed in sap-srs-qui.

    4. Run BigApplication. Debug, using standard debugging tools such as gdb(1) for C code, and Prolog debugging tools for Prolog code.

Quintus Prolog provides defaults for interfacing the operating system. If customization is necessary in this area, a user must completely redefine, not just extend, the supplied functions.

Please note: The default OS interface functions that can be redefined all have names beginning with the prefix QU_.

The QU_ functions are like hooks in the sense that they provide you with a place to insert code that changes Prolog's behavior. However, we do not include Prolog hooks such as message_hook/3 in this discussion because the point of embedding is to call Prolog code from foreign programs. The Prolog hooks are used independently of embedding.