Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > 29d4b60c68c6b2dac6b16939691f8a13 > files > 5

ocaml-doc-4.01.0-3.mga4.noarch.rpm

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="generator" content="hevea 2.00">
<link rel="stylesheet" type="text/css" href="manual.css">
<title>Batch compilation (ocamlc)</title>
</head>
<body>
<a href="extn.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="toplevel.html"><img src="next_motif.gif" alt="Next"></a>
<hr>
<h1 class="chapter" id="sec241">Chapter&#XA0;8&#XA0;&#XA0;Batch compilation (ocamlc)</h1>
<ul>
<li><a href="comp.html#sec242">Overview of the compiler</a>
</li><li><a href="comp.html#sec243">Options</a>
</li><li><a href="comp.html#sec244">Modules and the file system</a>
</li><li><a href="comp.html#sec245">Common errors</a>
</li></ul>
<p> <a id="c:camlc"></a>

</p><p>This chapter describes the OCaml batch compiler <span class="c007">ocamlc</span>,
which compiles OCaml source files to bytecode object files and links
these object files to produce standalone bytecode executable files.
These executable files are then run by the bytecode interpreter
<span class="c007">ocamlrun</span>.</p>
<h2 class="section" id="sec242">8.1&#XA0;&#XA0;Overview of the compiler</h2>
<p>The <span class="c007">ocamlc</span> command has a command-line interface similar to the one of
most C compilers. It accepts several types of arguments and processes them
sequentially:</p><ul class="itemize"><li class="li-itemize">
Arguments ending in <span class="c007">.mli</span> 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 <span class="c013">x</span><span class="c007">.mli</span>, the <span class="c007">ocamlc</span> compiler produces a compiled interface
in the file <span class="c013">x</span><span class="c007">.cmi</span>.</li><li class="li-itemize">Arguments ending in <span class="c007">.ml</span> 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 <span class="c013">x</span><span class="c007">.ml</span>, the <span class="c007">ocamlc</span>
compiler produces compiled object bytecode in the file <span class="c013">x</span><span class="c007">.cmo</span>.<p>If the interface file <span class="c013">x</span><span class="c007">.mli</span> exists, the implementation
<span class="c013">x</span><span class="c007">.ml</span> is checked against the corresponding compiled interface
<span class="c013">x</span><span class="c007">.cmi</span>, which is assumed to exist. If no interface
<span class="c013">x</span><span class="c007">.mli</span> is provided, the compilation of <span class="c013">x</span><span class="c007">.ml</span> produces a
compiled interface file <span class="c013">x</span><span class="c007">.cmi</span> in addition to the compiled
object code file <span class="c013">x</span><span class="c007">.cmo</span>. The file <span class="c013">x</span><span class="c007">.cmi</span> produced
corresponds to an interface that exports everything that is defined in
the implementation <span class="c013">x</span><span class="c007">.ml</span>.</p></li><li class="li-itemize">Arguments ending in <span class="c007">.cmo</span> are taken to be compiled object bytecode. These
files are linked together, along with the object files obtained
by compiling <span class="c007">.ml</span> arguments (if any), and the OCaml standard
library, to produce a standalone executable program. The order in
which <span class="c007">.cmo</span> and <span class="c007">.ml</span> 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 <span class="c013">x</span><span class="c007">.cmo</span> file must come
before all <span class="c007">.cmo</span> files that refer to the unit <span class="c013">x</span>.</li><li class="li-itemize">Arguments ending in <span class="c007">.cma</span> are taken to be libraries of object bytecode.
A library of object bytecode packs in a single file a set of object
bytecode files (<span class="c007">.cmo</span> files). Libraries are built with <span class="c007">ocamlc -a</span>
(see the description of the <span class="c007">-a</span> option below). The object files
contained in the library are linked as regular <span class="c007">.cmo</span> files (see
above), in the order specified when the <span class="c007">.cma</span> 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.</li><li class="li-itemize">Arguments ending in <span class="c007">.c</span> are passed to the C compiler, which generates
a <span class="c007">.o</span> object file (<span class="c007">.obj</span> under Windows). This object file is linked
with the program if the <span class="c007">-custom</span> flag is set (see the description of
<span class="c007">-custom</span> below).</li><li class="li-itemize">Arguments ending in <span class="c007">.o</span> or <span class="c007">.a</span> (<span class="c007">.obj</span> or <span class="c007">.lib</span> under Windows)
are assumed to be C object files and libraries. They are passed to the
C linker when linking in <span class="c007">-custom</span> mode (see the description of
<span class="c007">-custom</span> below).</li><li class="li-itemize">Arguments ending in <span class="c007">.so</span> (<span class="c007">.dll</span> under Windows)
are assumed to be C shared libraries (DLLs). During linking, they are
searched for external C functions referenced from the OCaml code,
and their names are written in the generated bytecode executable.
The run-time system <span class="c007">ocamlrun</span> then loads them dynamically at program
start-up time.</li></ul><p>The output of the linking phase is a file containing compiled bytecode
that can be executed by the OCaml bytecode interpreter:
the command named <span class="c007">ocamlrun</span>. If <span class="c007">a.out</span> is the name of the file
produced by the linking phase, the command
</p><pre>
        ocamlrun a.out <span class="c013">arg</span><sub>1</sub> <span class="c013">arg</span><sub>2</sub> &#X2026; <span class="c013">arg</span><sub><span class="c013">n</span></sub>
</pre><p>
executes the compiled code contained in <span class="c007">a.out</span>, passing it as
arguments the character strings <span class="c013">arg</span><sub>1</sub> to <span class="c013">arg</span><sub><span class="c013">n</span></sub>.
(See chapter&#XA0;<a href="runtime.html#c%3Aruntime">10</a> for more details.)</p><p>On most systems, the file produced by the linking
phase can be run directly, as in:
</p><pre>
        ./a.out <span class="c013">arg</span><sub>1</sub> <span class="c013">arg</span><sub>2</sub> &#X2026; <span class="c013">arg</span><sub><span class="c013">n</span></sub>
</pre><p>
The produced file has the executable bit set, and it manages to launch
the bytecode interpreter by itself.</p>
<h2 class="section" id="sec243">8.2&#XA0;&#XA0;Options</h2>
<p><a id="s:comp-options"></a></p><p>The following command-line options are recognized by <span class="c007">ocamlc</span>.
The options <span class="c007">-pack</span>, <span class="c007">-a</span>, <span class="c007">-c</span> and <span class="c007">-output-obj</span> are mutually exclusive.</p><dl class="description"><dt class="dt-description"><span class="c010">-a</span></dt><dd class="dd-description">
Build a library (<span class="c007">.cma</span> file) with the object files (<span class="c007">.cmo</span> files)
given on the command line, instead of linking them into an executable
file. The name of the library must be set with the <span class="c007">-o</span> option.<p>If <span class="c007">-custom</span>, <span class="c007">-cclib</span> or <span class="c007">-ccopt</span> options are passed on the command
line, these options are stored in the resulting <span class="c007">.cma</span> library. Then,
linking with this library automatically adds back the <span class="c007">-custom</span>,
<span class="c007">-cclib</span> and <span class="c007">-ccopt</span> options as if they had been provided on the
command line, unless the <span class="c007">-noautolink</span> option is given.</p></dd><dt class="dt-description"><span class="c010">-absname</span></dt><dd class="dd-description">
Force error messages to show absolute paths for file names.</dd><dt class="dt-description"><span class="c010">-annot</span></dt><dd class="dd-description">
Dump detailed information about the compilation (types, bindings,
tail-calls, etc). The information for file <span class="c013">src</span><span class="c007">.ml</span>
is put into file <span class="c013">src</span><span class="c007">.annot</span>. In case of a type error, dump
all the information inferred by the type-checker before the error.
The <span class="c013">src</span><span class="c007">.annot</span> file can be used with the emacs commands given in
<span class="c007">emacs/caml-types.el</span> to display types and other annotations
interactively.</dd><dt class="dt-description"><span class="c010">-c</span></dt><dd class="dd-description">
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.</dd><dt class="dt-description"><span class="c019"><span class="c007">-cc</span> <span class="c013">ccomp</span></span></dt><dd class="dd-description">
Use <span class="c013">ccomp</span> as the C linker when linking in &#X201C;custom runtime&#X201D;
mode (see the <span class="c007">-custom</span> option)
and as the C compiler for compiling <span class="c007">.c</span> source files.</dd><dt class="dt-description"><span class="c019"><span class="c007">-cclib</span> <span class="c007">-l</span><span class="c013">libname</span></span></dt><dd class="dd-description">
Pass the <span class="c007">-l</span><span class="c013">libname</span> option to the C linker when linking in
&#X201C;custom runtime&#X201D; mode (see the <span class="c007">-custom</span> option). This causes the
given C library to be linked with the program.</dd><dt class="dt-description"><span class="c019"><span class="c007">-ccopt</span> <span class="c013">option</span></span></dt><dd class="dd-description">
Pass the given option to the C compiler and linker. When linking in
&#X201C;custom runtime&#X201D; mode, for instance,
<span class="c007">-ccopt -L</span><span class="c013">dir</span> causes the C linker to search for C libraries in
directory <span class="c013">dir</span>. (See the <span class="c007">-custom</span> option.)</dd><dt class="dt-description"><span class="c010">-config</span></dt><dd class="dd-description">
Print the version number of <span class="c007">ocamlc</span> and a detailed summary of its
configuration, then exit.</dd><dt class="dt-description"><span class="c010">-custom</span></dt><dd class="dd-description">
Link in &#X201C;custom runtime&#X201D; mode. In the default linking mode, the
linker produces bytecode that is intended to be executed with the
shared runtime system, <span class="c007">ocamlrun</span>. 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 <span class="c007">ocamlrun</span> command is not
installed. Moreover, the &#X201C;custom runtime&#X201D; mode enables static
linking of OCaml code with user-defined C functions, as described in
chapter&#XA0;<a href="intfc.html#sec404">19</a>.
<blockquote class="quote"><span class="c011">Unix:</span>&#XA0;&#XA0;
Never use the <span class="c007">strip</span> command on executables produced by <span class="c007">ocamlc -custom</span>,
this would remove the bytecode part of the executable.
</blockquote></dd><dt class="dt-description"><span class="c019"><span class="c007">-dllib</span> <span class="c007">-l</span><span class="c013">libname</span></span></dt><dd class="dd-description">
Arrange for the C shared library <span class="c007">dll</span><span class="c013">libname</span><span class="c007">.so</span>
(<span class="c007">dll</span><span class="c013">libname</span><span class="c007">.dll</span> under Windows) to be loaded dynamically
by the run-time system <span class="c007">ocamlrun</span> at program start-up time.</dd><dt class="dt-description"><span class="c019"><span class="c007">-dllpath</span> <span class="c013">dir</span></span></dt><dd class="dd-description">
Adds the directory <span class="c013">dir</span> 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 <span class="c007">-I</span> option).
The <span class="c007">-dllpath</span> option simply stores <span class="c013">dir</span> in the produced
executable file, where <span class="c007">ocamlrun</span> can find it and use it as
described in section&#XA0;<a href="runtime.html#s-ocamlrun-dllpath">10.3</a>.</dd><dt class="dt-description"><span class="c010">-g</span></dt><dd class="dd-description">
Add debugging information while compiling and linking. This option is
required in order to be able to debug the program with <span class="c007">ocamldebug</span>
(see chapter&#XA0;<a href="debugger.html#c%3Adebugger">16</a>), and to produce stack backtraces when
the program terminates on an uncaught exception (see
section&#XA0;<a href="runtime.html#ocamlrun-options">10.2</a>).</dd><dt class="dt-description"><span class="c010">-i</span></dt><dd class="dd-description">
Cause the compiler to print all defined names (with their inferred
types or their definitions) when compiling an implementation (<span class="c007">.ml</span>
file). No compiled files (<span class="c007">.cmo</span> and <span class="c007">.cmi</span> files) are produced.
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 (<span class="c007">.mli</span> file) for a file:
just redirect the standard output of the compiler to a <span class="c007">.mli</span> file,
and edit that file to remove all declarations of unexported names.</dd><dt class="dt-description"><span class="c019"><span class="c007">-I</span> <span class="c013">directory</span></span></dt><dd class="dd-description">
Add the given directory to the list of directories searched for
compiled interface files (<span class="c007">.cmi</span>), compiled object code files
(<span class="c007">.cmo</span>), libraries (<span class="c007">.cma</span>), and C libraries specified with
<span class="c007">-cclib -lxxx</span>. By default, the current directory is
searched first, then the standard library directory. Directories added
with <span class="c007">-I</span> are searched after the current directory, in the order in
which they were given on the command line, but before the standard
library directory.<p>If the given directory starts with <span class="c007">+</span>, it is taken relative to the
standard library directory. For instance, <span class="c007">-I +labltk</span> adds the
subdirectory <span class="c007">labltk</span> of the standard library to the search path.</p></dd><dt class="dt-description"><span class="c019"><span class="c007">-impl</span> <span class="c013">filename</span></span></dt><dd class="dd-description">
Compile the file <span class="c013">filename</span> as an implementation file, even if its
extension is not <span class="c007">.ml</span>.</dd><dt class="dt-description"><span class="c019"><span class="c007">-intf</span> <span class="c013">filename</span></span></dt><dd class="dd-description">
Compile the file <span class="c013">filename</span> as an interface file, even if its
extension is not <span class="c007">.mli</span>.</dd><dt class="dt-description"><span class="c019"><span class="c007">-intf-suffix</span> <span class="c013">string</span></span></dt><dd class="dd-description">
Recognize file names ending with <span class="c013">string</span> as interface files
(instead of the default <span class="c007">.mli</span>).</dd><dt class="dt-description"><span class="c010">-labels</span></dt><dd class="dd-description">
Labels are not ignored in types, labels may be used in applications,
and labelled parameters can be given in any order. This is the default.</dd><dt class="dt-description"><span class="c010">-linkall</span></dt><dd class="dd-description">
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 (option <span class="c007">-a</span>), setting the <span class="c007">-linkall</span> option forces all
subsequent links of programs involving that library to link all the
modules contained in the library.</dd><dt class="dt-description"><span class="c010">-make-runtime</span></dt><dd class="dd-description">
Build a custom runtime system (in the file specified by option <span class="c007">-o</span>)
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
<span class="c007">ocamlc -use-runtime</span> <span class="c013">runtime-name</span> option.
See section&#XA0;<a href="intfc.html#s%3Acustom-runtime">19.1.6</a> for more information.</dd><dt class="dt-description"><span class="c010">-noassert</span></dt><dd class="dd-description">
Do not compile assertion checks. Note that the special form
<span class="c007">assert false</span> is always compiled because it is typed specially.
This flag has no effect when linking already-compiled files.</dd><dt class="dt-description"><span class="c010">-noautolink</span></dt><dd class="dd-description">
When linking <span class="c007">.cma</span> libraries, ignore <span class="c007">-custom</span>, <span class="c007">-cclib</span> and <span class="c007">-ccopt</span>
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 <span class="c007">-noautolink</span> and pass the correct C
libraries and options on the command line.</dd><dt class="dt-description"><span class="c010">-nolabels</span></dt><dd class="dd-description">
Ignore non-optional labels in types. Labels cannot be used in
applications, and parameter order becomes strict.</dd><dt class="dt-description"><span class="c019"><span class="c007">-o</span> <span class="c013">exec-file</span></span></dt><dd class="dd-description">
Specify the name of the output file produced by the compiler. The
default output name is <span class="c007">a.out</span> under Unix and <span class="c007">camlprog.exe</span> under
Windows. If the <span class="c007">-a</span> option is given, specify the name of the library
produced. If the <span class="c007">-pack</span> option is given, specify the name of the
packed object file produced. If the <span class="c007">-output-obj</span> option is given,
specify the name of the output file produced. If the <span class="c007">-c</span> option is
given, specify the name of the object file produced for the <em>next</em>
source file that appears on the command line.</dd><dt class="dt-description"><span class="c010">-output-obj</span></dt><dd class="dd-description">
Cause the linker to produce a C object file instead of a bytecode
executable file. This is useful to wrap OCaml code as a C library,
callable from any C program. See chapter&#XA0;<a href="intfc.html#sec404">19</a>,
section&#XA0;<a href="intfc.html#s%3Aembedded-code">19.7.5</a>. The name of the output object file
must be set with the <span class="c007">-o</span> option. This
option can also be used to produce a C source file (<span class="c007">.c</span> extension) or
a compiled shared/dynamic library (<span class="c007">.so</span> extension, <span class="c007">.dll</span> under Windows).</dd><dt class="dt-description"><span class="c010">-pack</span></dt><dd class="dd-description">
Build a bytecode object file (<span class="c007">.cmo</span> file) and its associated compiled
interface (<span class="c007">.cmi</span>) that combines the object
files given on the command line, making them appear as sub-modules of
the output <span class="c007">.cmo</span> file. The name of the output <span class="c007">.cmo</span> file must be
given with the <span class="c007">-o</span> option. For instance,
<pre>        ocamlc -pack -o p.cmo a.cmo b.cmo c.cmo
</pre>generates compiled files <span class="c007">p.cmo</span> and <span class="c007">p.cmi</span> describing a compilation
unit having three sub-modules <span class="c007">A</span>, <span class="c007">B</span> and <span class="c007">C</span>, corresponding to the
contents of the object files <span class="c007">a.cmo</span>, <span class="c007">b.cmo</span> and <span class="c007">c.cmo</span>. These
contents can be referenced as <span class="c007">P.A</span>, <span class="c007">P.B</span> and <span class="c007">P.C</span> in the remainder
of the program.</dd><dt class="dt-description"><span class="c019"><span class="c007">-pp</span> <span class="c013">command</span></span></dt><dd class="dd-description">
Cause the compiler to call the given <span class="c013">command</span> as a preprocessor
for each source file. The output of <span class="c013">command</span> is redirected to
an intermediate file, which is compiled. If there are no compilation
errors, the intermediate file is deleted afterwards.</dd><dt class="dt-description"><span class="c010">-principal</span></dt><dd class="dd-description">
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 <span class="c007">-principal</span> mode are also accepted in the
default mode with equivalent types, but different binary signatures,
and this may slow down type checking; yet it is a good idea to
use it once before publishing source code.</dd><dt class="dt-description"><span class="c010">-rectypes</span></dt><dd class="dd-description">
Allow arbitrary recursive types during type-checking. By default,
only recursive types where the recursion goes through an object type
are supported. Note that once you have created an interface using this
flag, you must use it again for all dependencies.</dd><dt class="dt-description"><span class="c019"><span class="c007">-runtime-variant</span> <span class="c013">suffix</span></span></dt><dd class="dd-description">
Add the <span class="c013">suffix</span> string to the name of the runtime library used by
the program. Currently, only one such suffix is supported: <span class="c007">d</span>, and
only if the OCaml compiler was configured with option
<span class="c007">-with-debug-runtime</span>. This suffix gives the debug version of the
runtime, which is useful for debugging pointer problems in low-level
code such as C stubs.</dd><dt class="dt-description"><span class="c010">-thread</span></dt><dd class="dd-description">
Compile or link multithreaded programs, in combination with the
system <span class="c007">threads</span> library described in chapter&#XA0;<a href="libthreads.html#c%3Athreads">25</a>.</dd><dt class="dt-description"><span class="c010">-unsafe</span></dt><dd class="dd-description">
Turn bound checking off for array and string accesses (the <span class="c007">v.(i)</span> and
<span class="c007">s.[i]</span> constructs). Programs compiled with <span class="c007">-unsafe</span> are therefore
slightly faster, but unsafe: anything can happen if the program
accesses an array or string outside of its bounds.</dd><dt class="dt-description"><span class="c019"><span class="c007">-use-runtime</span> <span class="c013">runtime-name</span></span></dt><dd class="dd-description">
Generate a bytecode executable file that can be executed on the custom
runtime system <span class="c013">runtime-name</span>, built earlier with
<span class="c007">ocamlc -make-runtime</span> <span class="c013">runtime-name</span>.
See section&#XA0;<a href="intfc.html#s%3Acustom-runtime">19.1.6</a> for more information.</dd><dt class="dt-description"><span class="c010">-v</span></dt><dd class="dd-description">
Print the version number of the compiler and the location of the
standard library directory, then exit.</dd><dt class="dt-description"><span class="c010">-verbose</span></dt><dd class="dd-description">
Print all external commands before they are executed, in particular
invocations of the C compiler and linker in <span class="c007">-custom</span> mode. Useful to
debug C library problems.</dd><dt class="dt-description"><span class="c019"><span class="c007">-vnum</span> or <span class="c007">-version</span></span></dt><dd class="dd-description">
Print the version number of the compiler in short form (e.g. <span class="c007">3.11.0</span>),
then exit.</dd><dt class="dt-description"><span class="c010">-vmthread</span></dt><dd class="dd-description">
Compile or link multithreaded programs, in combination with the
VM-level <span class="c007">threads</span> library described in chapter&#XA0;<a href="libthreads.html#c%3Athreads">25</a>.</dd><dt class="dt-description"><span class="c019"><span class="c007">-w</span> <span class="c013">warning-list</span></span></dt><dd class="dd-description">
Enable, disable, or mark as errors the warnings specified by the argument
<span class="c013">warning-list</span>.
Each warning can be <em>enabled</em> or <em>disabled</em>, and each warning
can be <em>marked</em> or <em>unmarked</em>.
If a warning is disabled, it isn&#X2019;t displayed and doesn&#X2019;t affect
compilation in any way (even if it is marked). If a warning is
enabled, it is displayed normally by the compiler whenever the source
code triggers it. If it is enabled and marked, the compiler will stop
with an error after displaying that warning if the source code
triggers it.<p>The <span class="c013">warning-list</span> argument is a sequence of warning specifiers,
with no separators between them. A warning specifier is one of the
following:</p><dl class="description"><dt class="dt-description">
<span class="c019"><span class="c007">+</span><span class="c013">num</span></span></dt><dd class="dd-description"> Enable warning number <span class="c013">num</span>.
</dd><dt class="dt-description"><span class="c019"><span class="c007">-</span><span class="c013">num</span></span></dt><dd class="dd-description"> Disable warning number <span class="c013">num</span>.
</dd><dt class="dt-description"><span class="c019"><span class="c007">@</span><span class="c013">num</span></span></dt><dd class="dd-description"> Enable and mark warning number <span class="c013">num</span>.
</dd><dt class="dt-description"><span class="c019"><span class="c007">+</span><span class="c013">num1</span>..<span class="c013">num2</span></span></dt><dd class="dd-description"> Enable warnings in the given range.
</dd><dt class="dt-description"><span class="c019"><span class="c007">-</span><span class="c013">num1</span>..<span class="c013">num2</span></span></dt><dd class="dd-description"> Disable warnings in the given range.
</dd><dt class="dt-description"><span class="c019"><span class="c007">@</span><span class="c013">num1</span>..<span class="c013">num2</span></span></dt><dd class="dd-description"> Enable and mark warnings in the given range.
</dd><dt class="dt-description"><span class="c019"><span class="c007">+</span><span class="c013">letter</span></span></dt><dd class="dd-description"> Enable the set of warnings corresponding to
<span class="c013">letter</span>. The letter may be uppercase or lowercase.
</dd><dt class="dt-description"><span class="c019"><span class="c007">-</span><span class="c013">letter</span></span></dt><dd class="dd-description"> Disable the set of warnings corresponding to
<span class="c013">letter</span>. The letter may be uppercase or lowercase.
</dd><dt class="dt-description"><span class="c019"><span class="c007">@</span><span class="c013">letter</span></span></dt><dd class="dd-description"> Enable and mark the set of warnings
corresponding to <span class="c013">letter</span>. The letter may be uppercase or
lowercase.
</dd><dt class="dt-description"><span class="c017">uppercase-letter</span></dt><dd class="dd-description"> Enable the set of warnings corresponding
to <span class="c013">uppercase-letter</span>.
</dd><dt class="dt-description"><span class="c017">lowercase-letter</span></dt><dd class="dd-description"> Disable the set of warnings corresponding
to <span class="c013">lowercase-letter</span>.
</dd></dl><p>Warning numbers and letters which are out of the range of warnings
that are currently defined are ignored. The warning are as follows.
</p><dl class="description"><dt class="dt-description">
<span class="c019">1</span></dt><dd class="dd-description"> Suspicious-looking start-of-comment mark.
</dd><dt class="dt-description"><span class="c019">2</span></dt><dd class="dd-description"> Suspicious-looking end-of-comment mark.
</dd><dt class="dt-description"><span class="c019">3</span></dt><dd class="dd-description"> Deprecated feature.
</dd><dt class="dt-description"><span class="c019">4</span></dt><dd class="dd-description"> Fragile pattern matching: matching that will remain complete even
if additional constructors are added to one of the variant types
matched.
</dd><dt class="dt-description"><span class="c019">5</span></dt><dd class="dd-description"> Partially applied function: expression whose result has function
type and is ignored.
</dd><dt class="dt-description"><span class="c019">6</span></dt><dd class="dd-description"> Label omitted in function application.
</dd><dt class="dt-description"><span class="c019">7</span></dt><dd class="dd-description"> Method overridden.
</dd><dt class="dt-description"><span class="c019">8</span></dt><dd class="dd-description"> Partial match: missing cases in pattern-matching.
</dd><dt class="dt-description"><span class="c019">9</span></dt><dd class="dd-description"> Missing fields in a record pattern.
</dd><dt class="dt-description"><span class="c019">10</span></dt><dd class="dd-description"> Expression on the left-hand side of a sequence that doesn&#X2019;t have type
<span class="c007">unit</span> (and that is not a function, see warning number 5).
</dd><dt class="dt-description"><span class="c019">11</span></dt><dd class="dd-description"> Redundant case in a pattern matching (unused match case).
</dd><dt class="dt-description"><span class="c019">12</span></dt><dd class="dd-description"> Redundant sub-pattern in a pattern-matching.
</dd><dt class="dt-description"><span class="c019">13</span></dt><dd class="dd-description"> Instance variable overridden.
</dd><dt class="dt-description"><span class="c019">14</span></dt><dd class="dd-description"> Illegal backslash escape in a string constant.
</dd><dt class="dt-description"><span class="c019">15</span></dt><dd class="dd-description"> Private method made public implicitly.
</dd><dt class="dt-description"><span class="c019">16</span></dt><dd class="dd-description"> Unerasable optional argument.
</dd><dt class="dt-description"><span class="c019">17</span></dt><dd class="dd-description"> Undeclared virtual method.
</dd><dt class="dt-description"><span class="c019">18</span></dt><dd class="dd-description"> Non-principal type.
</dd><dt class="dt-description"><span class="c019">19</span></dt><dd class="dd-description"> Type without principality.
</dd><dt class="dt-description"><span class="c019">20</span></dt><dd class="dd-description"> Unused function argument.
</dd><dt class="dt-description"><span class="c019">21</span></dt><dd class="dd-description"> Non-returning statement.
</dd><dt class="dt-description"><span class="c019">22</span></dt><dd class="dd-description"> Camlp4 warning.
</dd><dt class="dt-description"><span class="c019">23</span></dt><dd class="dd-description"> Useless record <span class="c007">with</span> clause.
</dd><dt class="dt-description"><span class="c019">24</span></dt><dd class="dd-description"> Bad module name: the source file name is not a valid OCaml module name.
</dd><dt class="dt-description"><span class="c019">25</span></dt><dd class="dd-description"> Pattern-matching with all clauses guarded. Exhaustiveness cannot be
checked.
</dd><dt class="dt-description"><span class="c019">26</span></dt><dd class="dd-description"> Suspicious unused variable: unused variable that is bound
with <span class="c007">let</span> or <span class="c007">as</span>, and doesn&#X2019;t start with an underscore (<span class="c007">_</span>)
character.
</dd><dt class="dt-description"><span class="c019">27</span></dt><dd class="dd-description"> Innocuous unused variable: unused variable that is not bound with
<span class="c007">let</span> nor <span class="c007">as</span>, and doesn&#X2019;t start with an underscore (<span class="c007">_</span>)
character.
</dd><dt class="dt-description"><span class="c019">28</span></dt><dd class="dd-description"> Wildcard pattern given as argument to a constant constructor.
</dd><dt class="dt-description"><span class="c019">29</span></dt><dd class="dd-description"> Unescaped end-of-line in a string constant (non-portable code).
</dd><dt class="dt-description"><span class="c019">30</span></dt><dd class="dd-description"> Two labels or constructors of the same name are defined in two
mutually recursive types.
</dd><dt class="dt-description"><span class="c019">31</span></dt><dd class="dd-description"> A module is linked twice in the same executable.
</dd><dt class="dt-description"><span class="c019">32</span></dt><dd class="dd-description"> Unused value declaration.
</dd><dt class="dt-description"><span class="c019">33</span></dt><dd class="dd-description"> Unused open statement.
</dd><dt class="dt-description"><span class="c019">34</span></dt><dd class="dd-description"> Unused type declaration.
</dd><dt class="dt-description"><span class="c019">35</span></dt><dd class="dd-description"> Unused for-loop index.
</dd><dt class="dt-description"><span class="c019">36</span></dt><dd class="dd-description"> Unused ancestor variable.
</dd><dt class="dt-description"><span class="c019">37</span></dt><dd class="dd-description"> Unused constructor.
</dd><dt class="dt-description"><span class="c019">38</span></dt><dd class="dd-description"> Unused exception constructor.
</dd><dt class="dt-description"><span class="c019">39</span></dt><dd class="dd-description"> Unused rec flag.
</dd><dt class="dt-description"><span class="c019">40</span></dt><dd class="dd-description"> Constructor or label name used out of scope.
</dd><dt class="dt-description"><span class="c019">41</span></dt><dd class="dd-description"> Ambiguous constructor or label name.
</dd><dt class="dt-description"><span class="c019">42</span></dt><dd class="dd-description"> Disambiguated constructor or label name.
</dd><dt class="dt-description"><span class="c019">43</span></dt><dd class="dd-description"> Nonoptional label applied as optional.
</dd><dt class="dt-description"><span class="c019">44</span></dt><dd class="dd-description"> Open statement shadows an already defined identifier.
</dd><dt class="dt-description"><span class="c019">45</span></dt><dd class="dd-description"> Open statement shadows an already defined label or constructor.
</dd><dt class="dt-description"><span class="c019">A</span></dt><dd class="dd-description"> All warnings.
</dd><dt class="dt-description"><span class="c019">C</span></dt><dd class="dd-description"> Set of warnings 1, 2.
</dd><dt class="dt-description"><span class="c019">D</span></dt><dd class="dd-description"> Synonym for warning 3.
</dd><dt class="dt-description"><span class="c019">E</span></dt><dd class="dd-description"> Synonym for warning 4.
</dd><dt class="dt-description"><span class="c019">F</span></dt><dd class="dd-description"> Synonym for warning 5.
</dd><dt class="dt-description"><span class="c019">K</span></dt><dd class="dd-description"> Set of warnings 32, 33, 34, 35, 36, 37, 38, 39.
</dd><dt class="dt-description"><span class="c019">L</span></dt><dd class="dd-description"> Synonym for warning 6.
</dd><dt class="dt-description"><span class="c019">M</span></dt><dd class="dd-description"> Synonym for warning 7.
</dd><dt class="dt-description"><span class="c019">P</span></dt><dd class="dd-description"> Synonym for warning 8.
</dd><dt class="dt-description"><span class="c019">R</span></dt><dd class="dd-description"> Synonym for warning 9.
</dd><dt class="dt-description"><span class="c019">S</span></dt><dd class="dd-description"> Synonym for warning 10.
</dd><dt class="dt-description"><span class="c019">U</span></dt><dd class="dd-description"> Set of warnings 11, 12.
</dd><dt class="dt-description"><span class="c019">V</span></dt><dd class="dd-description"> Synonym for warning 13.
</dd><dt class="dt-description"><span class="c019">X</span></dt><dd class="dd-description"> Set of warnings 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 30.
</dd><dt class="dt-description"><span class="c019">Y</span></dt><dd class="dd-description"> Synonym for warning 26.
</dd><dt class="dt-description"><span class="c019">Z</span></dt><dd class="dd-description"> Synonym for warning 27.

</dd></dl><p>The default setting is <span class="c007">-w +a-4-6-7-9-27-29-32..39-41..42-44</span>.
It is displayed by <span class="c007">ocamlc -help</span>.
Note that warnings 5 and 10 are not always triggered, depending on
the internals of the type checker.</p></dd><dt class="dt-description"><span class="c019"><span class="c007">-warn-error</span> <span class="c013">warning-list</span></span></dt><dd class="dd-description">
Mark as errors the warnings specified in the argument <span class="c013">warning-list</span>.
The compiler will stop with an error when one of these warnings is
emitted. The <span class="c013">warning-list</span> has the same meaning as for
the <span class="c007">-w</span> option: a <span class="c007">+</span> sign (or an uppercase letter) turns the
corresponding warnings into errors, a <span class="c007">-</span>
sign (or a lowercase letter) turns them back into warnings, and a
<span class="c007">@</span> sign both enables and marks the corresponding warnings.<p>Note: it is not recommended to use warning sets (i.e. letters) as
arguments to <span class="c007">-warn-error</span>
in production code, because this can break your build when future versions
of OCaml add some new warnings.</p><p>The default setting is <span class="c007">-warn-error -a</span>
(none of the warnings is treated as an error).</p></dd><dt class="dt-description"><span class="c010">-warn-help</span></dt><dd class="dd-description">
Show description for all available warning numbers.</dd><dt class="dt-description"><span class="c010">-where</span></dt><dd class="dd-description">
Print the location of the standard library, then exit.</dd><dt class="dt-description"><span class="c019"><span class="c007">-</span> <span class="c013">file</span></span></dt><dd class="dd-description">
Process <span class="c013">file</span> as a file name, even if it starts with a dash (<span class="c007">-</span>)
character.</dd><dt class="dt-description"><span class="c019"><span class="c007">-help</span> or <span class="c007">--help</span></span></dt><dd class="dd-description">
Display a short usage summary and exit.
</dd></dl>
<h2 class="section" id="sec244">8.3&#XA0;&#XA0;Modules and the file system</h2>
<p>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.</p><p>The compiler always derives the module name by taking the capitalized
base name of the source file (<span class="c007">.ml</span> or <span class="c007">.mli</span> file). That is, it
strips the leading directory name, if any, as well as the <span class="c007">.ml</span> or
<span class="c007">.mli</span> 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 <span class="c007">mylib/misc.ml</span> provides an
implementation for the module named <span class="c007">Misc</span>. Other compilation units
may refer to components defined in <span class="c007">mylib/misc.ml</span> under the names
<span class="c007">Misc.</span><span class="c013">name</span>; they can also do <span class="c007">open Misc</span>, then use unqualified
names <span class="c013">name</span>.</p><p>The <span class="c007">.cmi</span> and <span class="c007">.cmo</span> 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 <span class="c007">.cmi</span> files) or implement
(for <span class="c007">.cmo</span> files).</p><p>When the compiler encounters a reference to a free module identifier
<span class="c007">Mod</span>, it looks in the search path for a file named <span class="c007">Mod.cmi</span> or <span class="c007">mod.cmi</span>
and loads the compiled interface
contained in that file. As a consequence, renaming <span class="c007">.cmi</span> files is not
advised: the name of a <span class="c007">.cmi</span> 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
<span class="c007">-I</span> options are given to the compiler. The compiler will flag an
error if it loads a <span class="c007">.cmi</span> file that has been renamed.</p><p>Compiled bytecode files (<span class="c007">.cmo</span> files), on the other hand, can be
freely renamed once created. That&#X2019;s because the linker never attempts
to find by itself the <span class="c007">.cmo</span> file that implements a module with a
given name: it relies instead on the user providing the list of <span class="c007">.cmo</span>
files by hand.</p>
<h2 class="section" id="sec245">8.4&#XA0;&#XA0;Common errors</h2>
<p> <a id="s:comp-errors"></a></p><p>This section describes and explains the most frequently encountered
error messages.</p><dl class="description"><dt class="dt-description"><span class="c019">Cannot find file <span class="c013">filename</span></span></dt><dd class="dd-description">
The named file could not be found in the current directory, nor in the
directories of the search path. The <span class="c013">filename</span> is either a
compiled interface file (<span class="c007">.cmi</span> file), or a compiled bytecode file
(<span class="c007">.cmo</span> file). If <span class="c013">filename</span> has the format <span class="c013">mod</span><span class="c007">.cmi</span>, this
means you are trying to compile a file that references identifiers
from module <span class="c013">mod</span>, but you have not yet compiled an interface for
module <span class="c013">mod</span>. Fix: compile <span class="c013">mod</span><span class="c007">.mli</span> or <span class="c013">mod</span><span class="c007">.ml</span>
first, to create the compiled interface <span class="c013">mod</span><span class="c007">.cmi</span>.<p>If <span class="c013">filename</span> has the format <span class="c013">mod</span><span class="c007">.cmo</span>, this
means you are trying to link a bytecode object file that does not
exist yet. Fix: compile <span class="c013">mod</span><span class="c007">.ml</span> first.</p><p>If your program spans several directories, this error can also appear
because you haven&#X2019;t specified the directories to look into. Fix: add
the correct <span class="c007">-I</span> options to the command line.</p></dd><dt class="dt-description"><span class="c019">Corrupted compiled interface <span class="c013">filename</span></span></dt><dd class="dd-description">
The compiler produces this error when it tries to read a compiled
interface file (<span class="c007">.cmi</span> file) that has the wrong structure. This means
something went wrong when this <span class="c007">.cmi</span> 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 <span class="c007">.cmi</span> file is modified after
its creation by the compiler. Fix: remove the corrupted <span class="c007">.cmi</span> file,
and rebuild it.</dd><dt class="dt-description"><span class="c019">This expression has type </span><span class="c013">t</span><sub>1</sub><span class="c019">, but is used with type </span><span class="c013">t</span><sub>2</sub></dt><dd class="dd-description">
This is by far the most common type error in programs. Type <span class="c013">t</span><sub>1</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 <span class="c013">t</span><sub>2</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 <span class="c013">t</span><sub>1</sub> and <span class="c013">t</span><sub>2</sub> are not
compatible, then the error above is produced.<p>In some cases, it is hard to understand why the two types <span class="c013">t</span><sub>1</sub> and
<span class="c013">t</span><sub>2</sub> are incompatible. For instance, the compiler can report that
&#X201C;expression of type <span class="c007">foo</span> cannot be used with type <span class="c007">foo</span>&#X201D;, and it
really seems that the two types <span class="c007">foo</span> 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:
</p><pre>        type foo = A | B
        let f = function A -&gt; 0 | B -&gt; 1
        type foo = C | D
        f C
</pre><p>This result in the error message &#X201C;expression <span class="c007">C</span> of type <span class="c007">foo</span> cannot
be used with type <span class="c007">foo</span>&#X201D;.</p></dd><dt class="dt-description"><span class="c019">The type of this expression, <span class="c013">t</span>, contains type variables
that cannot be generalized</span></dt><dd class="dd-description">
Type variables (<span class="c007">'a</span>, <span class="c007">'b</span>, &#X2026;) in a type <span class="c013">t</span> can be in either
of two states: generalized (which means that the type <span class="c013">t</span> is valid
for all possible instantiations of the variables) and not generalized
(which means that the type <span class="c013">t</span> is valid only for one instantiation
of the variables). In a <span class="c007">let</span> binding <span class="c007">let </span><span class="c013">name</span><span class="c007"> = </span><span class="c013">expr</span>,
the type-checker normally generalizes as many type variables as
possible in the type of <span class="c013">expr</span>. 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
<span class="c007">let</span> bindings only if the bound expression <span class="c013">expr</span> belongs to the
class of &#X201C;syntactic values&#X201D;, which includes constants, identifiers,
functions, tuples of syntactic values, etc. In all other cases (for
instance, <span class="c013">expr</span> is a function application), a polymorphic mutable
could have been created and generalization is therefore turned off for
all variables occuring in contravariant or non-variant branches of the
type. For instance, if the type of a non-value is <span class="c007">'a list</span> the
variable is generalizable (<span class="c007">list</span> is a covariant type constructor),
but not in <span class="c007">'a list -&gt; 'a list</span> (the left branch of <span class="c007">-&gt;</span> is
contravariant) or <span class="c007">'a ref</span> (<span class="c007">ref</span> is non-variant).<p>Non-generalized type variables in a type cause no difficulties inside
a given structure or compilation unit (the contents of a <span class="c007">.ml</span> file,
or an interactive session), but they cannot be allowed inside
signatures nor in compiled interfaces (<span class="c007">.cmi</span> file), because they
could be used inconsistently later. Therefore, the compiler
flags an error when a structure or compilation unit defines a value
<span class="c013">name</span> whose type contains non-generalized type variables. There
are two ways to fix this error:
</p><ul class="itemize"><li class="li-itemize">
Add a type constraint or a <span class="c007">.mli</span> file to give a monomorphic
type (without type variables) to <span class="c013">name</span>. 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><li class="li-itemize">If you really need <span class="c013">name</span> 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></li></ul></dd><dt class="dt-description"><span class="c019">Reference to undefined global <span class="c013">mod</span></span></dt><dd class="dd-description">
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 <span class="c013">mod</span> on the command line
(typically, the file named <span class="c013">mod</span><span class="c007">.cmo</span>, or a library containing
that file). Fix: add the missing <span class="c007">.ml</span> or <span class="c007">.cmo</span> file to the command
line. Or, you have provided an implementation for the module named
<span class="c013">mod</span>, but it comes too late on the command line: the
implementation of <span class="c013">mod</span> must come before all bytecode object files
that reference <span class="c013">mod</span>. Fix: change the order of <span class="c007">.ml</span> and <span class="c007">.cmo</span>
files on the command line.<p>Of course, you will always encounter this error if you have mutually
recursive functions across modules. That is, function <span class="c007">Mod1.f</span> calls
function <span class="c007">Mod2.g</span>, and function <span class="c007">Mod2.g</span> calls function <span class="c007">Mod1.f</span>.
In this case, no matter what permutations you perform on the command
line, the program will be rejected at link-time. Fixes:
</p><ul class="itemize"><li class="li-itemize">
Put <span class="c007">f</span> and <span class="c007">g</span> in the same module.
</li><li class="li-itemize">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 <span class="c007">mod1.cmo</span> before <span class="c007">mod2.cmo</span>.
</li><li class="li-itemize">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></li></ul></dd><dt class="dt-description"><span class="c019">The external function <span class="c013">f</span> is not available</span></dt><dd class="dd-description">
This error appears when trying to link code that calls external
functions written in C. As explained in
chapter&#XA0;<a href="intfc.html#sec404">19</a>, such code must be linked with C libraries that
implement the required <span class="c013">f</span> C function. If the C libraries in
question are not shared libraries (DLLs), the code must be linked in
&#X201C;custom runtime&#X201D; mode. Fix: add the required C libraries to the
command line, and possibly the <span class="c007">-custom</span> option.</dd></dl>
<hr>
<a href="extn.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="toplevel.html"><img src="next_motif.gif" alt="Next"></a>
</body>
</html>