Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > 19bb6433bb07a8b16410504336791904 > files > 229

ocaml-doc-3.06-5mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14">
<TITLE>
 Batch compilation (ocamlc)
</TITLE>
</HEAD>
<BODY TEXT=black BGCOLOR=white>
<A HREF="manual021.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="manual023.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#2de52d"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc98"><B><FONT SIZE=6>Chapter&nbsp;8</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=6>Batch compilation (ocamlc)</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE> <A NAME="c:camlc"></A>
<BR>
This chapter describes the Objective Caml batch compiler <TT>ocamlc</TT>,
which compiles Caml source files to bytecode object files and link
these object files to produce standalone bytecode executable files.
These executable files are then run by the bytecode interpreter
<TT>ocamlrun</TT>.<BR>
<BR>
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc99"><B><FONT SIZE=5>8.1</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Overview of the compiler</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE><BR>
The <TT>ocamlc</TT> command has a command-line interface similar to the one of
most C compilers. It accepts several types of arguments:
<UL><LI>
Arguments ending in <TT>.mli</TT> are taken to be source files for
compilation unit interfaces. Interfaces specify the names exported by
compilation units: they declare value names with their types, define
public data types, declare abstract data types, and so on. From the
file <I>x</I><TT>.mli</TT>, the <TT>ocamlc</TT> compiler produces a compiled interface
in the file <I>x</I><TT>.cmi</TT>.<BR>
<BR>
<LI>Arguments ending in <TT>.ml</TT> are taken to be source files for compilation
unit implementations. Implementations provide definitions for the
names exported by the unit, and also contain expressions to be
evaluated for their side-effects. From the file <I>x</I><TT>.ml</TT>, the <TT>ocamlc</TT>
compiler produces compiled object bytecode in the file <I>x</I><TT>.cmo</TT>. <BR>
<BR>
If the interface file <I>x</I><TT>.mli</TT> exists, the implementation
<I>x</I><TT>.ml</TT> is checked against the corresponding compiled interface
<I>x</I><TT>.cmi</TT>, which is assumed to exist. If no interface
<I>x</I><TT>.mli</TT> is provided, the compilation of <I>x</I><TT>.ml</TT> produces a
compiled interface file <I>x</I><TT>.cmi</TT> in addition to the compiled
object code file <I>x</I><TT>.cmo</TT>. The file <I>x</I><TT>.cmi</TT> produced
corresponds to an interface that exports everything that is defined in
the implementation <I>x</I><TT>.ml</TT>.<BR>
<BR>
<LI>Arguments ending in <TT>.cmo</TT> are taken to be compiled object bytecode. These
files are linked together, along with the object files obtained
by compiling <TT>.ml</TT> arguments (if any), and the Objective Caml standard
library, to produce a standalone executable program. The order in
which <TT>.cmo</TT> and <TT>.ml</TT> arguments are presented on the command line is
relevant: compilation units are initialized in that order at
run-time, and it is a link-time error to use a component of a unit
before having initialized it. Hence, a given <I>x</I><TT>.cmo</TT> file must come
before all <TT>.cmo</TT> files that refer to the unit <I>x</I>.<BR>
<BR>
<LI>Arguments ending in <TT>.cma</TT> are taken to be libraries of object bytecode.
A library of object bytecode packs in a single file a set of object
bytecode files (<TT>.cmo</TT> files). Libraries are built with <TT>ocamlc -a</TT>
(see the description of the <TT>-a</TT> option below). The object files
contained in the library are linked as regular <TT>.cmo</TT> files (see
above), in the order specified when the <TT>.cma</TT> file was built. The
only difference is that if an object file contained in a library is
not referenced anywhere in the program, then it is not linked in.<BR>
<BR>
<LI>Arguments ending in <TT>.c</TT> are passed to the C compiler, which generates
a <TT>.o</TT> object file. This object file is linked with the program if the
<TT>-custom</TT> flag is set (see the description of <TT>-custom</TT> below).<BR>
<BR>
<LI>Arguments ending in <TT>.o</TT> or <TT>.a</TT> (<TT>.obj</TT> or <TT>.lib</TT> under Windows)
are assumed to be C object files and libraries. They are passed to the
C linker when linking in <TT>-custom</TT> mode (see the description of
<TT>-custom</TT> below).<BR>
<BR>
<LI>Arguments ending in <TT>.so</TT> (<TT>.dll</TT> under Windows)
are assumed to be C shared libraries (DLLs). During linking, they are
searched for external C functions referenced from the Caml code,
and their names are written in the generated bytecode executable.
The run-time system <TT>ocamlrun</TT> then loads them dynamically at program
start-up time.</UL>
The output of the linking phase is a file containing compiled bytecode
that can be executed by the Objective Caml bytecode interpreter:
the command named <TT>ocamlrun</TT>. If <TT>caml.out</TT> is the name of the file
produced by the linking phase, the command
<PRE>
        ocamlrun caml.out <I>arg</I><SUB><FONT SIZE=2>1</FONT></SUB> <I>arg</I><SUB><FONT SIZE=2>2</FONT></SUB> ... <I>arg</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB>
</PRE>
executes the compiled code contained in <TT>caml.out</TT>, passing it as
arguments the character strings <I>arg</I><SUB><FONT SIZE=2>1</FONT></SUB> to <I>arg</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB>.
(See chapter&nbsp;<A HREF="manual024.html#c:runtime">10</A> for more details.)<BR>
<BR>
On most systems, the file produced by the linking
phase can be run directly, as in:
<PRE>
        ./caml.out <I>arg</I><SUB><FONT SIZE=2>1</FONT></SUB> <I>arg</I><SUB><FONT SIZE=2>2</FONT></SUB> ... <I>arg</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB>
</PRE>
The produced file has the executable bit set, and it manages to launch
the bytecode interpreter by itself.<BR>
<BR>
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc100"><B><FONT SIZE=5>8.2</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Options</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE><BR>
The following command-line options are recognized by <TT>ocamlc</TT>.
<DL COMPACT=compact><DT><B><TT>-a</TT></B><DD>
Build a library (<TT>.cma</TT> file) with the object files (<TT>.cmo</TT> files)
given on the command line, instead of linking them into an executable
file. The name of the library can be set with the <TT>-o</TT> option. The
default name is <TT>library.cma</TT>. <BR>
<BR>
If <TT>-custom</TT>, <TT>-cclib</TT> or <TT>-ccopt</TT> options are passed on the command
line, these options are stored in the resulting <TT>.cma</TT> library. Then,
linking with this library automatically adds back the <TT>-custom</TT>,
<TT>-cclib</TT> and <TT>-ccopt</TT> options as if they had been provided on the
command line, unless the <TT>-noautolink</TT> option is given.<BR>
<BR>
<DT><B><TT>-c</TT></B><DD>
Compile only. Suppress the linking phase of the
compilation. Source code files are turned into compiled files, but no
executable file is produced. This option is useful to
compile modules separately.<BR>
<BR>
<DT><B><TT>-cc</TT> <I>ccomp</I></B><DD>
Use <I>ccomp</I> as the C linker called by <TT>ocamlc -custom</TT>
and as the C compiler for compiling <TT>.c</TT> source files.<BR>
<BR>
<DT><B><TT>-cclib</TT> <TT>-l</TT><I>libname</I></B><DD>
Pass the <TT>-l</TT><I>libname</I> option to the C linker when linking in
``custom runtime'' mode (see the <TT>-custom</TT> option). This causes the
given C library to be linked with the program.<BR>
<BR>
<DT><B><TT>-ccopt</TT> <I>option</I></B><DD>
Pass the given option to the C compiler and linker, when linking in
``custom runtime'' mode (see the <TT>-custom</TT> option). For instance,
<TT>-ccopt -L</TT><I>dir</I> causes the C linker to search for C libraries in
directory <I>dir</I>.<BR>
<BR>
<DT><B><TT>-custom</TT></B><DD>
Link in ``custom runtime'' mode. In the default linking mode, the
linker produces bytecode that is intended to be executed with the
shared runtime system, <TT>ocamlrun</TT>. In the custom runtime mode, the
linker produces an output file that contains both the runtime system
and the bytecode for the program. The resulting file is larger, but it
can be executed directly, even if the <TT>ocamlrun</TT> command is not
installed. Moreover, the ``custom runtime'' mode enables static
linking of Caml code with user-defined C functions, as described in
chapter&nbsp;<A HREF="manual032.html#c:intf-c">18</A>.

&nbsp;&nbsp;<FONT COLOR=purple>Unix:</FONT>
<BLOCKQUOTE>
Never use the <TT>strip</TT> command on executables produced by <TT>ocamlc -custom</TT>.
This would remove the bytecode part of the executable.
</BLOCKQUOTE><BR>
<BR>
<DT><B><TT>-dllib</TT> <TT>-l</TT><I>libname</I></B><DD>
Arrange for the C shared library <TT>dll</TT><I>libname</I><TT>.so</TT>
(<TT>dll</TT><I>libname</I><TT>.dll</TT> under Windows) to be loaded dynamically
by the run-time system <TT>ocamlrun</TT> at program start-up time.<BR>
<BR>
<DT><B><TT>-dllpath</TT> <I>dir</I></B><DD>
Adds the directory <I>dir</I> to the run-time search path for shared
C libraries. At link-time, shared libraries are searched in the
standard search path (the one corresponding to the <TT>-I</TT> option).
The <TT>-dllpath</TT> option simply stores <I>dir</I> in the produced
executable file, where <TT>ocamlrun</TT> can find it and exploit it as
described in section&nbsp;<A HREF="manual024.html#s-ocamlrun-dllpath">10.3</A>.<BR>
<BR>
<DT><B><TT>-g</TT></B><DD>
Add debugging information while compiling and linking. This option is
required in order to be able to debug the program with <TT>ocamldebug</TT>
(see chapter&nbsp;<A HREF="manual030.html#c:debugger">16</A>).<BR>
<BR>
<DT><B><TT>-i</TT></B><DD>
Cause the compiler to print all defined names (with their inferred
types or their definitions) when compiling an implementation (<TT>.ml</TT>
file). This can be useful to check the types inferred by the
compiler. Also, since the output follows the syntax of interfaces, it
can help in writing an explicit interface (<TT>.mli</TT> file) for a file:
just redirect the standard output of the compiler to a <TT>.mli</TT> file,
and edit that file to remove all declarations of unexported names.<BR>
<BR>
<DT><B><TT>-I</TT> <I>directory</I></B><DD>
Add the given directory to the list of directories searched for
compiled interface files (<TT>.cmi</TT>), compiled object code files
(<TT>.cmo</TT>), libraries (<TT>.cma</TT>), and C libraries specified with
<TT>-cclib -lxxx</TT>. By default, the current directory is
searched first, then the standard library directory. Directories added
with <TT>-I</TT> are searched after the current directory, in the order in
which they were given on the command line, but before the standard
library directory.<BR>
<BR>
If the given directory starts with <TT>+</TT>, it is taken relative to the
standard library directory. For instance, <TT>-I +labltk</TT> adds the
subdirectory <TT>labltk</TT> of the standard library to the search path.<BR>
<BR>
<DT><B><TT>-impl</TT> <I>filename</I></B><DD>
Compile the file <I>filename</I> as an implementation file, even if its
extension is not <TT>.ml</TT>.<BR>
<BR>
<DT><B><TT>-intf</TT> <I>filename</I></B><DD>
Compile the file <I>filename</I> as an interface file, even if its
extension is not <TT>.mli</TT>.<BR>
<BR>
<DT><B><TT>-linkall</TT></B><DD>
Force all modules contained in libraries to be linked in. If this
flag is not given, unreferenced modules are not linked in. When
building a library (<TT>-a</TT> flag), setting the <TT>-linkall</TT> flag forces all
subsequent links of programs involving that library to link all the
modules contained in the library.<BR>
<BR>
<DT><B><TT>-make-runtime</TT></B><DD>
Build a custom runtime system (in the file specified by option <TT>-o</TT>)
incorporating the C object files and libraries given on the command
line. This custom runtime system can be used later to execute
bytecode executables produced with the
<TT>ocamlc -use-runtime</TT> <I>runtime-name</I> option.
See section&nbsp;<A HREF="manual032.html#s:custom-runtime">18.1.6</A> for more information.<BR>
<BR>
<DT><B><TT>-noassert</TT></B><DD>
Turn assertion checking off: assertions are not compiled.
This flag has no effect when linking already compiled files.<BR>
<BR>
<DT><B><TT>-noautolink</TT></B><DD>
When linking <TT>.cma</TT> libraries, ignore <TT>-custom</TT>, <TT>-cclib</TT> and <TT>-ccopt</TT>
options potentially contained in the libraries (if these options were
given when building the libraries). This can be useful if a library
contains incorrect specifications of C libraries or C options; in this
case, during linking, set <TT>-noautolink</TT> and pass the correct C
libraries and options on the command line.<BR>
<BR>
<DT><B><TT>-nolabels</TT></B><DD>
Ignore non-optional labels in types. Labels cannot be used in
applications, and parameter order becomes strict.<BR>
<BR>
<DT><B><TT>-o</TT> <I>exec-file</I></B><DD>
Specify the name of the output file produced by the linker. The
default output name is <TT>a.out</TT>, in keeping with the Unix tradition. If
the <TT>-a</TT> option is given, specify the name of the library produced.
If the <TT>-output-obj</TT> option is given, specify the name of the output
file produced.<BR>
<BR>
<DT><B><TT>-output-obj</TT></B><DD>
Cause the linker to produce a C object file instead of a bytecode
executable file. This is useful to wrap Caml code as a C library,
callable from any C program. See chapter&nbsp;<A HREF="manual032.html#c:intf-c">18</A>,
section&nbsp;<A HREF="manual032.html#s:embedded-code">18.7.5</A>. The name of the output object file is
<TT>camlprog.o</TT> by default; it can be set with the <TT>-o</TT> option.<BR>
<BR>
<DT><B><TT>-pack</TT></B><DD>
Build a bytecode object file (<TT>.cmo</TT> file) and its associated compiled
interface (<TT>.cmi</TT>) that combines the object
files given on the command line, making them appear as sub-modules of
the output <TT>.cmo</TT> file. The name of the output <TT>.cmo</TT> file must be
given with the <TT>-o</TT> option. For instance,
<PRE>
        ocamlc -pack -o p.cmo a.cmo b.cmo c.cmo
</PRE>generates compiled files <TT>p.cmo</TT> and <TT>p.cmi</TT> describing a compilation
unit having three sub-modules <TT>A</TT>, <TT>B</TT> and <TT>C</TT>, corresponding to the
contents of the object files <TT>a.cmo</TT>, <TT>b.cmo</TT> and <TT>c.cmo</TT>. These
contents can be referenced as <TT>P.A</TT>, <TT>P.B</TT> and <TT>P.C</TT> in the remainder
of the program.<BR>
<BR>
<DT><B><TT>-pp</TT> <I>command</I></B><DD>
Cause the compiler to call the given <I>command</I> as a preprocessor
for each source file. The output of <I>command</I> is redirected to
an intermediate file, which is compiled. If there are no compilation
errors, the intermediate file is deleted afterwards. The name of this
file is built from the basename of the source file with the extension
<TT>.ppi</TT> for an interface (<TT>.mli</TT>) file and <TT>.ppo</TT> for an implementation
(<TT>.ml</TT>) file.<BR>
<BR>
<DT><B><TT>-principal</TT></B><DD>
Check information path during type-checking, to make sure that all
types are derived in a principal way. When using labelled arguments
and/or polymorphic methods, this flag is required to ensure future
versions of the compiler will be able to infer types correctly, even
if internal algorithms change.
All programs accepted in <TT>-principal</TT> mode are also accepted in
default mode with equivalent types, but different binary signatures,
and this may slow down type checking; yet this is a good idea to
use it once before publishing source code.<BR>
<BR>
<DT><B><TT>-rectypes</TT></B><DD>
Allow arbitrary recursive types during type-checking. By default,
only recursive types where the recursion goes through an object type
are supported.<BR>
<BR>
<DT><B><TT>-thread</TT></B><DD>
Compile or link multithreaded programs, in combination with the
<TT>threads</TT> library described in chapter&nbsp;<A HREF="manual038.html#c:threads">24</A>. What this
option actually does is select a special, thread-safe version of the
standard library.<BR>
<BR>
<DT><B><TT>-unsafe</TT></B><DD>
Turn bound checking off on array and string accesses (the <TT>v.(i)</TT> and
<TT>s.[i]</TT> constructs). Programs compiled with <TT>-unsafe</TT> are therefore
slightly faster, but unsafe: anything can happen if the program
accesses an array or string outside of its bounds.<BR>
<BR>
<DT><B><TT>-use-runtime</TT> <I>runtime-name</I></B><DD>
Generate a bytecode executable file that can be executed on the custom
runtime system <I>runtime-name</I>, built earlier with
<TT>ocamlc -make-runtime</TT> <I>runtime-name</I>. 
See section&nbsp;<A HREF="manual032.html#s:custom-runtime">18.1.6</A> for more information.<BR>
<BR>
<DT><B><TT>-v</TT></B><DD>
Print the version number of the compiler and the location of the
standard library directory, then exit.<BR>
<BR>
<DT><B><TT>-verbose</TT></B><DD>
Print all external commands before they are executed, in particular
invocations of the C compiler and linker in <TT>-custom</TT> mode. Useful to
debug C library problems.<BR>
<BR>
<DT><B><TT>-version</TT></B><DD>
Print the version number of the compiler in short form (e.g. <TT>3.06</TT>),
then exit.<BR>
<BR>
<DT><B><TT>-w</TT> <I>warning-list</I></B><DD>
Enable or disable warnings according to the argument
<I>warning-list</I>. The argument is a string of one or several
characters, with the following meaning for each character:
<DL COMPACT=compact><DT>
<B><TT>A</TT>/<TT>a</TT></B><DD> enable/disable all warnings.
<DT><B><TT>C</TT>/<TT>c</TT></B><DD> enable/disable warnings for suspicious comments.
<DT><B><TT>D</TT>/<TT>d</TT></B><DD> enable/disable warnings for deprecated features.
<DT><B><TT>F</TT>/<TT>f</TT></B><DD> enable/disable warnings for partially applied functions
(i.e. <TT>f x; </TT><I>expr</I> where the application <TT>f x</TT> has a function type).
<DT><B><TT>L</TT>/<TT>l</TT></B><DD> enable/disable warnings for labels omitted in application.
<DT><B><TT>M</TT>/<TT>m</TT></B><DD> enable/disable warnings for overriden methods.
<DT><B><TT>P</TT>/<TT>p</TT></B><DD> enable/disable warnings for partial matches (missing cases
in pattern matchings).
<DT><B><TT>S</TT>/<TT>s</TT></B><DD> enable/disable warnings for statements that do not have
type <TT>unit</TT> (e.g. <I>expr1</I><TT>; </TT><I>expr2</I> when <I>expr1</I> does not
have type <TT>unit</TT>).
<DT><B><TT>U</TT>/<TT>u</TT></B><DD> enable/disable warnings for unused (redundant) match cases.
<DT><B><TT>V</TT>/<TT>v</TT></B><DD> enable/disable warnings for hidden instance variables.
<DT><B><TT>X</TT>/<TT>x</TT></B><DD> enable/disable all other warnings.
</DL>
The default setting is <TT>-w Al</TT> (all warnings but labels enabled).<BR>
<BR>
<DT><B><TT>-warn-error</TT> <I>warning-list</I></B><DD>
Turn the warnings indicated in the argument <I>warning-list</I> into
errors. The compiler will stop on an error as soon as one of these
warnings is emitted, instead of going on. The <I>warning-list</I>
is a string of one or several characters, with the same meaning as for
the <TT>-w</TT> option: an uppercase character turns the corresponding
warning into an error, a lowercase character leaves it as a warning.
The default setting is <TT>-warn-error a</TT> (all warnings are not treated
as errors).<BR>
<BR>
<DT><B><TT>-where</TT></B><DD>
Print the location of the standard library, then exit.
</DL>
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc101"><B><FONT SIZE=5>8.3</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Modules and the file system</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE><BR>
This short section is intended to clarify the relationship between the
names of the modules corresponding to compilation units and the names
of the files that contain their compiled interface and compiled
implementation.<BR>
<BR>
The compiler always derives the module name by taking the capitalized
base name of the source file (<TT>.ml</TT> or <TT>.mli</TT> file). That is, it
strips the leading directory name, if any, as well as the <TT>.ml</TT> or
<TT>.mli</TT> suffix; then, it set the first letter to uppercase, in order to
comply with the requirement that module names must be capitalized.
For instance, compiling the file <TT>mylib/misc.ml</TT> provides an
implementation for the module named <TT>Misc</TT>. Other compilation units
may refer to components defined in <TT>mylib/misc.ml</TT> under the names
<TT>Misc.</TT><I>name</I>; they can also do <TT>open Misc</TT>, then use unqualified
names <I>name</I>.<BR>
<BR>
The <TT>.cmi</TT> and <TT>.cmo</TT> files produced by the compiler have the same
base name as the source file. Hence, the compiled files always have
their base name equal (modulo capitalization of the first letter) to
the name of the module they describe (for <TT>.cmi</TT> files) or implement
(for <TT>.cmo</TT> files).<BR>
<BR>
When the compiler encounters a reference to a free module identifier
<TT>Mod</TT>, it looks in the search path for a file <TT>mod.cmi</TT> (note
lowercasing of first letter) and loads the compiled interface
contained in that file. As a consequence, renaming <TT>.cmi</TT> files is not
advised: the name of a <TT>.cmi</TT> file must always correspond to the name
of the compilation unit it implements. It is admissible to move them
to another directory, if their base name is preserved, and the correct
<TT>-I</TT> options are given to the compiler. The compiler will flag an
error if it loads a <TT>.cmi</TT> file that has been renamed.<BR>
<BR>
Compiled bytecode files (<TT>.cmo</TT> files), on the other hand, can be
freely renamed once created. That's because the linker never attempts
to find by itself the <TT>.cmo</TT> file that implements a module with a
given name: it relies instead on the user providing the list of <TT>.cmo</TT>
files by hand.<BR>
<BR>
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc102"><B><FONT SIZE=5>8.4</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Common errors</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE> <A NAME="s:comp-errors"></A><BR>
This section describes and explains the most frequently encountered
error messages.
<DL COMPACT=compact><DT><B>Cannot find file <I>filename</I></B><DD>
The named file could not be found in the current directory, nor in the
directories of the search path. The <I>filename</I> is either a
compiled interface file (<TT>.cmi</TT> file), or a compiled bytecode file
(<TT>.cmo</TT> file). If <I>filename</I> has the format <I>mod</I><TT>.cmi</TT>, this
means you are trying to compile a file that references identifiers
from module <I>mod</I>, but you have not yet compiled an interface for
module <I>mod</I>. Fix: compile <I>mod</I><TT>.mli</TT> or <I>mod</I><TT>.ml</TT>
first, to create the compiled interface <I>mod</I><TT>.cmi</TT>. <BR>
<BR>
If <I>filename</I> has the format <I>mod</I><TT>.cmo</TT>, this
means you are trying to link a bytecode object file that does not
exist yet. Fix: compile <I>mod</I><TT>.ml</TT> first.<BR>
<BR>
If your program spans several directories, this error can also appear
because you haven't specified the directories to look into. Fix: add
the correct <TT>-I</TT> options to the command line.<BR>
<BR>
<DT><B>Corrupted compiled interface <I>filename</I></B><DD>
The compiler produces this error when it tries to read a compiled
interface file (<TT>.cmi</TT> file) that has the wrong structure. This means
something went wrong when this <TT>.cmi</TT> file was written: the disk was
full, the compiler was interrupted in the middle of the file creation,
and so on. This error can also appear if a <TT>.cmi</TT> file is modified after
its creation by the compiler. Fix: remove the corrupted <TT>.cmi</TT> file,
and rebuild it.<BR>
<BR>
<DT><B>This expression has type <I>t</I></B><SUB><B><FONT SIZE=2>1</FONT></B></SUB><B>, but is used with type <I>t</I></B><SUB><B><FONT SIZE=2>2</FONT></B></SUB><DD>
This is by far the most common type error in programs. Type <I>t</I><SUB><FONT SIZE=2>1</FONT></SUB> is
the type inferred for the expression (the part of the program that is
displayed in the error message), by looking at the expression itself.
Type <I>t</I><SUB><FONT SIZE=2>2</FONT></SUB> is the type expected by the context of the expression; it
is deduced by looking at how the value of this expression is used in
the rest of the program. If the two types <I>t</I><SUB><FONT SIZE=2>1</FONT></SUB> and <I>t</I><SUB><FONT SIZE=2>2</FONT></SUB> are not
compatible, then the error above is produced.<BR>
<BR>
In some cases, it is hard to understand why the two types <I>t</I><SUB><FONT SIZE=2>1</FONT></SUB> and
<I>t</I><SUB><FONT SIZE=2>2</FONT></SUB> are incompatible. For instance, the compiler can report that
``expression of type <TT>foo</TT> cannot be used with type <TT>foo</TT>'', and it
really seems that the two types <TT>foo</TT> are compatible. This is not
always true. Two type constructors can have the same name, but
actually represent different types. This can happen if a type
constructor is redefined. Example:
<PRE>
        type foo = A | B
        let f = function A -&gt; 0 | B -&gt; 1
        type foo = C | D
        f C
</PRE>This result in the error message ``expression <TT>C</TT> of type <TT>foo</TT> cannot
be used with type <TT>foo</TT>''. <BR>
<BR>
<DT><B>The type of this expression, <I>t</I></B><B>, contains type variables
 that cannot be generalized</B><DD>
Type variables (<TT>'a</TT>, <TT>'b</TT>, ...) in a type <I>t</I> can be in either
of two states: generalized (which means that the type <I>t</I> is valid
for all possible instantiations of the variables) and not generalized
(which means that the type <I>t</I> is valid only for one instantiation
of the variables). In a <TT>let</TT> binding <TT>let </TT><I>name</I><TT> = </TT><I>expr</I>,
the type-checker normally generalizes as many type variables as
possible in the type of <I>expr</I>. However, this leads to unsoundness
(a well-typed program can crash) in conjunction with polymorphic
mutable data structures. To avoid this, generalization is performed at
<TT>let</TT> bindings only if the bound expression <I>expr</I> belongs to the
class of ``syntactic values'', which includes constants, identifiers,
functions, tuples of syntactic values, etc. In all other cases (for
instance, <I>expr</I> is a function application), a polymorphic mutable
could have been created and generalization is therefore turned off.<BR>
<BR>
Non-generalized type variables in a type cause no difficulties inside
a given structure or compilation unit (the contents of a <TT>.ml</TT> file,
or an interactive session), but they cannot be allowed inside
signatures nor in compiled interfaces (<TT>.cmi</TT> file), because they
could be used inconsistently later. Therefore, the compiler
flags an error when a structure or compilation unit defines a value
<I>name</I> whose type contains non-generalized type variables. There
are two ways to fix this error:
<UL><LI>
Add a type constraint or a <TT>.mli</TT> file to give a monomorphic
type (without type variables) to <I>name</I>. For instance, instead of
writing
<PRE>
    let sort_int_list = Sort.list (&lt;)
    (* inferred type 'a list -&gt; 'a list, with 'a not generalized *)
</PRE>write
<PRE>
    let sort_int_list = (Sort.list (&lt;) : int list -&gt; int list);;
</PRE><LI>If you really need <I>name</I> to have a polymorphic type, turn
its defining expression into a function by adding an extra parameter.
For instance, instead of writing
<PRE>
    let map_length = List.map Array.length
    (* inferred type 'a array list -&gt; int list, with 'a not generalized *)
</PRE>write
<PRE>
    let map_length lv = List.map Array.length lv
</PRE></UL><BR>
<BR>
<DT><B>Reference to undefined global <I>mod</I></B><DD>
This error appears when trying to link an incomplete or incorrectly
ordered set of files. Either you have forgotten to provide an
implementation for the compilation unit named <I>mod</I> on the command line
(typically, the file named <I>mod</I><TT>.cmo</TT>, or a library containing
that file). Fix: add the missing <TT>.ml</TT> or <TT>.cmo</TT> file to the command
line. Or, you have provided an implementation for the module named
<I>mod</I>, but it comes too late on the command line: the
implementation of <I>mod</I> must come before all bytecode object files
that reference <I>mod</I>. Fix: change the order of <TT>.ml</TT> and <TT>.cmo</TT>
files on the command line.<BR>
<BR>
Of course, you will always encounter this error if you have mutually
recursive functions across modules. That is, function <TT>Mod1.f</TT> calls
function <TT>Mod2.g</TT>, and function <TT>Mod2.g</TT> calls function <TT>Mod1.f</TT>.
In this case, no matter what permutations you perform on the command
line, the program will be rejected at link-time. Fixes:
<UL><LI>
Put <TT>f</TT> and <TT>g</TT> in the same module.
<LI>Parameterize one function by the other.
That is, instead of having
<PRE>
mod1.ml:    let f x = ... Mod2.g ...
mod2.ml:    let g y = ... Mod1.f ...
</PRE>define
<PRE>
mod1.ml:    let f g x = ... g ...
mod2.ml:    let rec g y = ... Mod1.f g ...
</PRE>and link <TT>mod1.cmo</TT> before <TT>mod2.cmo</TT>.
<LI>Use a reference to hold one of the two functions, as in :
<PRE>
mod1.ml:    let forward_g =
                ref((fun x -&gt; failwith "forward_g") : &lt;type&gt;)
            let f x = ... !forward_g ...
mod2.ml:    let g y = ... Mod1.f ...
            let _ = Mod1.forward_g := g
</PRE>This will not work if <TT>g</TT> is a polymorphic function, however.
</UL><BR>
<BR>
<DT><B>The external function <I>f</I></B><B> is not available</B><DD>
This error appears when trying to link code that calls external
functions written in C. As explained in
chapter&nbsp;<A HREF="manual032.html#c:intf-c">18</A>, such code must be linked with C libraries that
implement the required <I>f</I> C function. If the C libraries in
question are not shared libraries (DLLs), the code must be linked in
``custom runtime'' mode. Fix: add the required C libraries to the
command line, and possibly the <TT>-custom</TT> option.</DL>


<HR>
<A HREF="manual021.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="manual023.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>