File: manual020.html

package info (click to toggle)
ocaml-doc 2.04-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,820 kB
  • ctags: 997
  • sloc: makefile: 38; sh: 12
file content (502 lines) | stat: -rw-r--r-- 23,853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<!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">
<TITLE>
 Batch compilation (ocamlc)
</TITLE>
</HEAD>
<BODY >
<A HREF="manual019.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual021.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>

<H1>Chapter&nbsp;7:&nbsp;&nbsp; Batch compilation (ocamlc)</H1> <A NAME="c:camlc"></A>
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>

<H2>7.1&nbsp;&nbsp; Overview of the compiler</H2>The <TT>ocamlc</TT> command has a command-line interface similar to the one of
most C compilers. It accepts several types of arguments:<BR>
<BR>
<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 Caml Light 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> 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).</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="manual022.html#c:runtime">9</A> for more details.)<BR>
<BR>
On most Unix 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>

<H2>7.2&nbsp;&nbsp; Options</H2>The following command-line options are recognized by <TT>ocamlc</TT>.<BR>
<BR>
<DL COMPACT=compact>
<DT><TT>-a</TT><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>

<DT><TT>-c</TT><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><TT>-cc</TT> <I>ccomp</I><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><TT>-cclib</TT> <TT>-l</TT><I>libname</I><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><TT>-ccopt</TT> <I>option</I><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><TT>-custom</TT><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 linking Caml
code with user-defined C functions, as described in
chapter&nbsp;<A HREF="manual028.html#c:intf-c">15</A>.<BR>
<BR>

<DT><TT>-g</TT><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="manual026.html#c:debugger">13</A>).<BR>
<BR>

<DT><TT>-i</TT><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><TT>-I</TT> <I>directory</I><DD>
Add the given directory to the list of directories searched for
compiled interface files (<TT>.cmi</TT>) and compiled object code files
(<TT>.cmo</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>

<DT><TT>-impl</TT> <I>filename</I><DD>
Compile the file <I>filename</I> as an implementation file, even if its
extension is not <TT>.ml</TT>.<BR>
<BR>

<DT><TT>-intf</TT> <I>filename</I><DD>
Compile the file <I>filename</I> as an interface file, even if its
extension is not <TT>.mli</TT>.<BR>
<BR>

<DT><TT>-linkall</TT><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><TT>-make-runtime</TT><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="manual028.html#s:custom-runtime">15.1.4</A> for more information.<BR>
<BR>

<DT><TT>-noassert</TT><DD>
Turn assertion checking off: assertions are not compiled.
This flag has no effect when linking already compiled files.<BR>
<BR>

<DT><TT>-o</TT> <I>exec-file</I><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><TT>-output-obj</TT><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="manual028.html#c:intf-c">15</A>,
section&nbsp;<A HREF="manual028.html#s:embedded-code">15.6.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><TT>-pp</TT> <I>command</I><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><TT>-rectypes</TT><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><TT>-thread</TT><DD>
Compile or link multithreaded programs, in combination with the
<TT>threads</TT> library described in chapter&nbsp;<A HREF="manual063.html#c:threads">21</A>. What this
option actually does is select a special, thread-safe version of the
standard library.<BR>
<BR>

<DT><TT>-unsafe</TT><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><TT>-use-runtime</TT> <I>runtime-name</I><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="manual028.html#s:custom-runtime">15.1.4</A> for more information.<BR>
<BR>

<DT><TT>-v</TT><DD>
Print the version number of the compiler.<BR>
<BR>

<DT><TT>-w</TT> <I>warning-list</I><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>
<TT>A</TT>/<TT>a</TT><DD> enable/disable all warnings

<DT><TT>F</TT>/<TT>f</TT><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><TT>M</TT>/<TT>m</TT><DD> enable/disable warnings for overriden methods.

<DT><TT>P</TT>/<TT>p</TT><DD> enable/disable warnings for partial matches (missing cases
in pattern matchings).

<DT><TT>S</TT>/<TT>s</TT><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><TT>U</TT>/<TT>u</TT><DD> enable/disable warnings for unused (redundant) match cases.

<DT><TT>V</TT>/<TT>v</TT><DD> enable/disable warnings for hidden instance variables.

<DT><TT>X</TT>/<TT>x</TT><DD> enable/disable all other warnings.
</DL>
The default setting is <TT>-w A</TT> (all warnings enabled).
</DL>
<H2>7.3&nbsp;&nbsp; Modules and the file system</H2>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>

<H2>7.4&nbsp;&nbsp; Common errors</H2> <A NAME="s:comp-errors"></A>This section describes and explains the most frequently encountered
error messages.<BR>
<BR>
<DL COMPACT=compact>
<DT>Cannot find file <I>filename</I><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>Corrupted compiled interface <I>filename</I><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>This expression has type <I>t</I><SUB><FONT SIZE=2>1</FONT></SUB>, but is used with type <I>t</I><SUB><FONT SIZE=2>2</FONT></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>The type of this expression, <I>t</I>, contains type variables
 that cannot be generalized<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>

<DT>Reference to undefined global <I>mod</I><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>

<DT>The external function <I>f</I> is not available<DD>
This error appears when trying to link code that calls external
functions written in C in ``default runtime'' mode. As explained in
chapter&nbsp;<A HREF="manual028.html#c:intf-c">15</A>, such code must be linked in ``custom runtime''
mode. Fix: add the <TT>-custom</TT> option, as well as the C libraries and C
object files that implement the required external functions.</DL>
<HR>
<A HREF="manual019.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual021.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>