OCAMLC(1) | General Commands Manual | OCAMLC(1) |
ocamlc - The OCaml bytecode compiler
ocamlc [ options ] filename ...
ocamlc.opt [ options ] filename ...
The OCaml bytecode compiler ocamlc(1) 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 ocamlrun(1).
The ocamlc(1) command has a command-line interface similar to the one of most C compilers. It accepts several types of arguments and processes them sequentially, after all options have been processed:
Arguments ending in .mli 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 x.mli, the ocamlc(1) compiler produces a compiled interface in the file x.cmi.
Arguments ending in .ml 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 x.ml, the ocamlc(1) compiler produces compiled object bytecode in the file x.cmo.
If the interface file x.mli exists, the implementation x.ml is checked against the corresponding compiled interface x.cmi, which is assumed to exist. If no interface x.mli is provided, the compilation of x.ml produces a compiled interface file x.cmi in addition to the compiled object code file x.cmo. The file x.cmi produced corresponds to an interface that exports everything that is defined in the implementation x.ml.
Arguments ending in .cmo are taken to be compiled object bytecode. These files are linked together, along with the object files obtained by compiling .ml arguments (if any), and the OCaml standard library, to produce a standalone executable program. The order in which .cmo and.ml 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 x.cmo file must come before all .cmo files that refer to the unit x.
Arguments ending in .cma are taken to be libraries of object bytecode. A library of object bytecode packs in a single file a set of object bytecode files (.cmo files). Libraries are built with ocamlc -a (see the description of the -a option below). The object files contained in the library are linked as regular .cmo files (see above), in the order specified when the .cma 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.
Arguments ending in .c are passed to the C compiler, which generates a .o object file. This object file is linked with the program if the -custom flag is set (see the description of -custom below).
Arguments ending in .o or .a are assumed to be C object files and libraries. They are passed to the C linker when linking in -custom mode (see the description of -custom below).
Arguments ending in .so 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 ocamlrun(1) then loads them dynamically at program start-up time.
The output of the linking phase is a file containing compiled bytecode that can be executed by the OCaml bytecode interpreter: the command ocamlrun(1). If caml.out is the name of the file produced by the linking phase, the command ocamlrun caml.out arg1 arg2 ... argn executes the compiled code contained in caml.out, passing it as arguments the character strings arg1 to argn. (See ocamlrun(1) for more details.)
On most systems, the file produced by the linking phase can be run directly, as in: ./caml.out arg1 arg2 ... argn. The produced file has the executable bit set, and it manages to launch the bytecode interpreter by itself.
ocamlc.opt is the same compiler as ocamlc, but compiled with the native-code compiler ocamlopt(1). Thus, it behaves exactly like ocamlc, but compiles faster. ocamlc.opt may not be available in all installations of OCaml.
The following command-line options are recognized by ocamlc(1).
auto use heuristics to enable colors only if the output supports them (an ANSI-compatible tty terminal);
always enable colors unconditionally;
never disable color output.
The default setting is auto, and the current heuristic checks that the "TERM" environment variable exists and is not empty or "dumb", and that isatty(stderr) holds.
The environment variable "OCAML_COLOR" is considered if -color is not provided. Its values are auto/always/never as above.
short only print the error and its location;
contextual like "short", but also display the source code snippet corresponding to the location of the error.
The default setting is contextual.
The environment variable "OCAML_ERROR_STYLE" is considered if -error-style is not provided. Its values are short/contextual as above.
Never use the strip(1) command on executables produced by ocamlc -custom, this would remove the bytecode part of the executable.
Security warning: never set the "setuid" or "setgid" bits on executables produced by ocamlc -custom, this would make them vulnerable to attacks.
If the given directory starts with +, it is taken relative to the standard library directory. For instance, -I +compiler-libs adds the subdirectory compiler-libs of the standard library to the search path.
Each warning can be enabled or disabled, and each warning can be fatal or non-fatal. If a warning is disabled, it isn't displayed and doesn't affect compilation in any way (even if it is fatal). If a warning is enabled, it is displayed normally by the compiler whenever the source code triggers it. If it is enabled and fatal, the compiler will also stop with an error after displaying it.
The warning-list argument is a sequence of warning specifiers, with no separators between them. A warning specifier is one of the following:
+num Enable warning number num.
-num Disable warning number num.
@num Enable and mark as fatal warning number num.
+num1..num2 Enable all warnings between num1 and num2 (inclusive).
-num1..num2 Disable all warnings between num1 and num2 (inclusive).
@num1..num2 Enable and mark as fatal all warnings between num1 and num2 (inclusive).
+letter Enable the set of warnings corresponding to letter. The letter may be uppercase or lowercase.
-letter Disable the set of warnings corresponding to letter. The letter may be uppercase or lowercase.
@letter Enable and mark as fatal the set of warnings corresponding to letter. The letter may be uppercase or lowercase.
uppercase-letter Enable the set of warnings corresponding to uppercase-letter.
lowercase-letter Disable the set of warnings corresponding to lowercase-letter.
The warning numbers are as follows.
1 Suspicious-looking start-of-comment mark.
2 Suspicious-looking end-of-comment mark.
3 Deprecated feature.
4 Fragile pattern matching: matching that will remain complete even if additional constructors are added to one of the variant types matched.
5 Partially applied function: expression whose result has function type and is ignored.
6 Label omitted in function application.
7 Method overridden without using the "method!" keyword.
8 Partial match: missing cases in pattern-matching.
9 Missing fields in a record pattern.
10 Expression on the left-hand side of a sequence that doesn't have type unit (and that is not a function, see warning number 5).
11 Redundant case in a pattern matching (unused match case).
12 Redundant sub-pattern in a pattern-matching.
13 Override of an instance variable.
14 Illegal backslash escape in a string constant.
15 Private method made public implicitly.
16 Unerasable optional argument.
17 Undeclared virtual method.
18 Non-principal type.
19 Type without principality.
20 Unused function argument.
21 Non-returning statement.
22 Preprocessor warning.
23 Useless record with clause.
24 Bad module name: the source file name is not a valid OCaml module name.
25 Deprecated: now part of warning 8.
26 Suspicious unused variable: unused variable that is bound with let or as, and doesn't start with an underscore (_) character.
27 Innocuous unused variable: unused variable that is not bound with let nor as, and doesn't start with an underscore (_) character.
28 A pattern contains a constant constructor applied to the underscore (_) pattern.
29 A non-escaped end-of-line was found in a string constant. This may cause portability problems between Unix and Windows.
30 Two labels or constructors of the same name are defined in two mutually recursive types.
31 A module is linked twice in the same executable.
32 Unused value declaration.
33 Unused open statement.
34 Unused type declaration.
35 Unused for-loop index.
36 Unused ancestor variable.
37 Unused constructor.
38 Unused extension constructor.
39 Unused rec flag.
40 Constructor or label name used out of scope.
41 Ambiguous constructor or label name.
42 Disambiguated constructor or label name.
43 Nonoptional label applied as optional.
44 Open statement shadows an already defined identifier.
45 Open statement shadows an already defined label or constructor.
46 Error in environment variable.
47 Illegal attribute payload.
48 Implicit elimination of optional arguments.
49 Missing cmi file when looking up module alias.
50 Unexpected documentation comment.
59 Assignment on non-mutable value.
60 Unused module declaration.
61 Unannotated unboxable type in primitive declaration.
62 Type constraint on GADT type declaration.
63 Erroneous printed signature.
64 -unsafe used with a preprocessor returning a syntax tree.
65 Type declaration defining a new '()' constructor.
66 Unused open! statement.
67 Unused functor parameter.
68 Pattern-matching depending on mutable state prevents the remaining arguments from being uncurried.
The letters stand for the following sets of warnings. Any letter not mentioned here corresponds to the empty set.
A all warnings
C 1, 2
D 3
E 4
F 5
K 32, 33, 34, 35, 36, 37, 38, 39
L 6
M 7
P 8
R 9
S 10
U 11, 12
V 13
X 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 30
Y 26
Z 27
Note: it is not recommended to use the -warn-error option in production code, because it will almost certainly prevent compiling your program with later versions of OCaml when they add new warnings or modify existing warnings.
The default setting is -warn-error -a+31 (only warning 31 is fatal).
ocamlopt(1), ocamlrun(1), ocaml(1).
The OCaml user's manual, chapter "Batch compilation".