Home Contents Index Summary Previous Next

2.7 Compilation

2.7.1 During program development

During program development, programs are normally loaded using consult/1, or the list abreviation. It is common practice to organise a project as a collection of source-files and a load-file, a Prolog file containing only use_module/[1,2] or ensure_loaded/1 directives, possibly with a definition of the entry-point of the program, the predicate that is normally used to start the program. This file is often called load.pl. If the entry-point is called go, a typical session starts as:

% pl <banner> 1 ?- [load]. <compilation messages> Yes 2 ?- go. <program interaction>

When using Windows, the user may open load.pl from the Windows explorer, which will cause plwin.exe to be started in the directory holding load.pl. Prolog loads load.pl before entering the toplevel.

2.7.2 For running the result

There are various options if you want to make your program ready for real usage. The best choice depends on whether the program is to be used only on machines holding the SWI-Prolog development system, the size of the program and the operating system (Unix vs. Windows).

2.7.2.1 Creating a shell-script

Especially on Unix systems and not-too-large applications, writing a shell-script that simply loads your application and calls the entry-point is often a good choice. A skeleton for the script is given below, followed by the Prolog code to obtain the program arguments.

#!/bin/sh base=<absolute-path-to-source> PL=pl exec $PL -f none -g "load_files(['$base/load'],[silent(true)])" -t go -- $*

go :- unix(argv(Arguments)), append(_SytemArgs, [--|Args], Arguments), !, go(Args). go(Args) :- ...

On Windows systems, similar behaviour can be achieved by creating a shortcut to Prolog, passing the proper options or writing a .bat file.

2.7.2.2 Creating a saved-state

For larger programs, as well as for programs that are required run on systems that do not have the SWI-Prolog development system installed, creating a saved state is the best solution. A saved state is created using qsave_program/[1,2] or using the linker plld(1). A saved state is a file containing machine-independent intermediate code in a format dedicated for fast loading. Optionally, the emulator may be integrated in the saved state, creating a single-file, but machine-dependent, exectable. This process is descriped in chapter 6.

2.7.2.3 Compililation using the -c commandline option

This mechanism is mostly for backward compatibility. It creates files in the same format as saved-states created using qsave_program/[1,2], but the resulting file is a translation of the source-files read, rather than a translation of the state of the machine. Unlike saved states, programs created using -c file do not include the Prolog part of the development system. The result is very dependent on the local SWI-Prolog installation.

The command below is used to compile one or more source-files.

pl [options] [-o output] -c file ...

The individual source files may include other files using the standard list notation, consult/1, ensure_loaded/1 and use_module/[1,2]. When the -o file option is omitted a file named a.out is created that holds the intermediate code file.

Intermediate code files start with the Unix magic code #! and are executable. This implies they can be started as a command:

% pl -o my_program -c ... ... % my_program [options]

Alternatively, my_program can be started with

% pl -x my_program [options]

The following restrictions apply to source files that are to be compiled with `-c':

Directives are executed both when compiling the program and when loading the intermediate code file.