[ Predicate Database and Compiler | The ECLiPSe Built-In Predicates | Reference Manual | Alphabetic Index ]

# compile(++File)

Compile specified file or list of files File.

File
  Atom, structure or string, or a list of them.

## Description

Compiles the specified Prolog source file or list of source files. The atom or string File must be instantiated to a legitimate specification for an existing file except for the suffix.

The predicate tries to add to it the Prolog suffixes from the list specified in the Prolog flag prolog_suffix and look for an existing and readable file. As soon as a file is found which exists, it is taken as the input file name. The default Prolog suffixes are empty suffix, .ecl and .pl.

If File is of the form library(Name), the predicates looks for the file in the directories from the library_path flag.

When File is not in the current directory, ECLiPSe first changes the current directory to that of the file. Consequently, recursive compile/1 calls inside compiled files can use relative pathnames. This makes it possible to compile the program top files from any directory and still use only local file names. At the end of the compilation, the current directory is changed back to the initial one. This has the side effect that all calls to cd/1 in queries in the compiled file are ignored.

Compiling a file which contains a module/1 or module/3 directive completely redefines an existing module of the same name. Otherwise, static procedures are redefined by those occurring in the compiled file, the clauses of dynamic procedures are appended to the existing ones. To add new procedures to an existing module without erasing the whole module the predicate compile/2 is to be used.

Note that the alternative notation [File1, File2, ... , FileN] may be used instead of compile([File1, File2,... , FileN]).

In addition to the exceptions listed below, any exception can occur during compilation, because general code may be executed in directives (:-/2), file queries (?-/2), macro transformations and inline expansion code.

### Modes and Determinism

- compile(++) is det

### Modules

This predicate is sensitive to its module context (tool predicate, see @/2).

### Exceptions

(4) instantiation fault
  File is not instantiated.
(5) type error
  File is instantiated, but not to an atom or string, or a list of atoms or strings.
(61) inconsistent tool redefinition
  A predicate that was already defined is later declared to be a tool.
(62) inconsistent procedure redefinition
  Illegal attempt to change a predicate's properties like: tool, dynamic, demon, parallel, calling convention.
(82) trying to access a locked module
  The module in which the clauses should be compiled is locked.
(94) trying to redefine an existing imported procedure
  There is already am imported predicate of the same name.
(130) syntax error: illegal head
  The head of a clause is not an atom or a compound term.
(131) syntax error: illegal goal
  A subgoal in the body of a clause is not an atom, a compound term or a variable.
(134) procedure clauses are not consecutive
  The clauses of a procedure are not consecutive.
(137) trying to redefine a procedure with another type
  A procedure which was previously referenced as built-in or external is now defined as a regular one, or vice versa.
(139) compiled or dumped file message
  This event is invoked at the end of each compiled file, by default it prints the compilation time and size of compiled code.
(143) compiled query failed
  A query in the compiled file has failed. This is by default ignored and the compilation continues.
(145) procedure being redefined in another file
  A procedure is being redefined in another file than the original one.
(147) compilation aborted
  This event is raised just before a compilation is aborted because of an error.
(148) bad pragma
  An unrecognised pragma was used in the code.
(171) File does not exist :
  File does not exist.

## Examples

    Success:
       [hanoi].         % compiles the file hanoi.pl

       [eclipse]: sh('cat file1').
       p:-q(hello).
       yes.
       [eclipse]: sh('cat file2').
       q(X) :- writeln(X).
       yes.
       [eclipse]: compile(user), p.
        p :- writeln(hi).
        user compiled 92 bytes in 0.00 seconds
       hi
       yes.
       [eclipse]: compile([file1, file2]), p.
       /home/lp/user/file1 compiled 32 bytes in 0.02 seconds
       /home/lp/user/file2 compiled 92 bytes in 0.00 seconds
       hello
       yes.

       % example showing use of relative pathnames.
       [eclipse]: sh('ls -FR /home/lp/user/pl').
       a.pl        util/

       /home/lp/user/pl/util:
       b.pl        c.pl

       yes.
       [eclipse]: sh('cat /home/lp/user/pl/a.pl').
       :- compile('util/b').
       p.

       yes.
       [eclipse]: compile('/home/lp/user/pl/a').
       /home/lp/user/pl/util/b.pl compiled 92 bytes in 0.00 seconds
       /home/lp/user/pl/a.pl compiled 28 bytes in 0.00 seconds
       yes.

    Error:
       compile(F).            (Error 4).
       compile(file1/1).      (Error 5).
       compile(file).         (Error 171).

## See Also

ensure_loaded / 1, compile / 2, . / 2, compile_stream / 1, compile_term / 1, lib / 1, set_flag / 2, use_module / 1, pragma / 1, fcompile : fcompile / 1