1OCAMLC(1) General Commands Manual OCAMLC(1)
2
3
4
6 ocamlc - The OCaml bytecode compiler
7
8
10 ocamlc [ options ] filename ...
11
12 ocamlc.opt [ options ] filename ...
13
14
16 The OCaml bytecode compiler ocamlc(1) compiles OCaml source files to
17 bytecode object files and links these object files to produce stand‐
18 alone bytecode executable files. These executable files are then run
19 by the bytecode interpreter ocamlrun(1).
20
21 The ocamlc(1) command has a command-line interface similar to the one
22 of most C compilers. It accepts several types of arguments and pro‐
23 cesses them sequentially, after all options have been processed:
24
25 Arguments ending in .mli are taken to be source files for compilation
26 unit interfaces. Interfaces specify the names exported by compilation
27 units: they declare value names with their types, define public data
28 types, declare abstract data types, and so on. From the file x.mli, the
29 ocamlc(1) compiler produces a compiled interface in the file x.cmi.
30
31 Arguments ending in .ml are taken to be source files for compilation
32 unit implementations. Implementations provide definitions for the names
33 exported by the unit, and also contain expressions to be evaluated for
34 their side-effects. From the file x.ml, the ocamlc(1) compiler pro‐
35 duces compiled object bytecode in the file x.cmo.
36
37 If the interface file x.mli exists, the implementation x.ml is checked
38 against the corresponding compiled interface x.cmi, which is assumed to
39 exist. If no interface x.mli is provided, the compilation of x.ml pro‐
40 duces a compiled interface file x.cmi in addition to the compiled
41 object code file x.cmo. The file x.cmi produced corresponds to an
42 interface that exports everything that is defined in the implementation
43 x.ml.
44
45 Arguments ending in .cmo are taken to be compiled object bytecode.
46 These files are linked together, along with the object files obtained
47 by compiling .ml arguments (if any), and the OCaml standard library, to
48 produce a standalone executable program. The order in which .cmo and.ml
49 arguments are presented on the command line is relevant: compilation
50 units are initialized in that order at run-time, and it is a link-time
51 error to use a component of a unit before having initialized it. Hence,
52 a given x.cmo file must come before all .cmo files that refer to the
53 unit x.
54
55 Arguments ending in .cma are taken to be libraries of object bytecode.
56 A library of object bytecode packs in a single file a set of object
57 bytecode files (.cmo files). Libraries are built with ocamlc -a (see
58 the description of the -a option below). The object files contained in
59 the library are linked as regular .cmo files (see above), in the order
60 specified when the .cma file was built. The only difference is that if
61 an object file contained in a library is not referenced anywhere in the
62 program, then it is not linked in.
63
64 Arguments ending in .c are passed to the C compiler, which generates a
65 .o object file. This object file is linked with the program if the
66 -custom flag is set (see the description of -custom below).
67
68 Arguments ending in .o or .a are assumed to be C object files and
69 libraries. They are passed to the C linker when linking in -custom mode
70 (see the description of -custom below).
71
72 Arguments ending in .so are assumed to be C shared libraries (DLLs).
73 During linking, they are searched for external C functions referenced
74 from the OCaml code, and their names are written in the generated byte‐
75 code executable. The run-time system ocamlrun(1) then loads them
76 dynamically at program start-up time.
77
78 The output of the linking phase is a file containing compiled bytecode
79 that can be executed by the OCaml bytecode interpreter: the command
80 ocamlrun(1). If caml.out is the name of the file produced by the link‐
81 ing phase, the command ocamlrun caml.out arg1 arg2 ... argn executes
82 the compiled code contained in caml.out, passing it as arguments the
83 character strings arg1 to argn. (See ocamlrun(1) for more details.)
84
85 On most systems, the file produced by the linking phase can be run
86 directly, as in: ./caml.out arg1 arg2 ... argn. The produced file has
87 the executable bit set, and it manages to launch the bytecode inter‐
88 preter by itself.
89
90 ocamlc.opt is the same compiler as ocamlc, but compiled with the
91 native-code compiler ocamlopt(1). Thus, it behaves exactly like
92 ocamlc, but compiles faster. ocamlc.opt may not be available in all
93 installations of OCaml.
94
95
97 The following command-line options are recognized by ocamlc(1).
98
99 -a Build a library (.cma file) with the object files (.cmo files)
100 given on the command line, instead of linking them into an exe‐
101 cutable file. The name of the library must be set with the -o
102 option.
103
104 If -custom, -cclib or -ccopt options are passed on the command
105 line, these options are stored in the resulting .cma library.
106 Then, linking with this library automatically adds back the
107 -custom, -cclib and -ccopt options as if they had been provided
108 on the command line, unless the -noautolink option is given.
109 Additionally, a substring $CAMLORIGIN inside a -ccopt options
110 will be replaced by the full path to the .cma library, excluding
111 the filename. -absname Show absolute filenames in error mes‐
112 sages.
113
114 -annot Dump detailed information about the compilation (types, bind‐
115 ings, tail-calls, etc). The information for file src.ml is put
116 into file src.annot. In case of a type error, dump all the
117 information inferred by the type-checker before the error. The
118 src.annot file can be used with the emacs commands given in
119 emacs/caml-types.el to display types and other annotations
120 interactively.
121
122 -bin-annot
123 Dump detailed information about the compilation (types, bind‐
124 ings, tail-calls, etc) in binary format. The information for
125 file src.ml is put into file src.cmt. In case of a type error,
126 dump all the information inferred by the type-checker before the
127 error. The annotation files produced by -bin-annot contain more
128 information and are much more compact than the files produced by
129 -annot.
130
131 -c Compile only. Suppress the linking phase of the compilation.
132 Source code files are turned into compiled files, but no exe‐
133 cutable file is produced. This option is useful to compile mod‐
134 ules separately.
135
136 -cc ccomp
137 Use ccomp as the C linker when linking in "custom runtime" mode
138 (see the -custom option) and as the C compiler for compiling .c
139 source files.
140
141 -cclib -llibname
142 Pass the -llibname option to the C linker when linking in "cus‐
143 tom runtime" mode (see the -custom option). This causes the
144 given C library to be linked with the program.
145
146 -ccopt option
147 Pass the given option to the C compiler and linker, when linking
148 in "custom runtime" mode (see the -custom option). For instance,
149 -ccopt -Ldir causes the C linker to search for C libraries in
150 directory dir.
151
152 -color mode
153 Enable or disable colors in compiler messages (especially warn‐
154 ings and errors). The following modes are supported:
155
156 auto use heuristics to enable colors only if the output supports
157 them (an ANSI-compatible tty terminal);
158
159 always enable colors unconditionally;
160
161 never disable color output.
162
163 The default setting is auto, and the current heuristic checks
164 that the "TERM" environment variable exists and is not empty or
165 "dumb", and that isatty(stderr) holds.
166
167 The environment variable "OCAML_COLOR" is considered if -color
168 is not provided. Its values are auto/always/never as above.
169
170
171 -compat-32
172 Check that the generated bytecode executable can run on 32-bit
173 platforms and signal an error if it cannot. This is useful when
174 compiling bytecode on a 64-bit machine.
175
176 -config
177 Print the version number of ocamlc(1) and a detailed summary of
178 its configuration, then exit.
179
180 -custom
181 Link in "custom runtime" mode. In the default linking mode, the
182 linker produces bytecode that is intended to be executed with
183 the shared runtime system, ocamlrun(1). In the custom runtime
184 mode, the linker produces an output file that contains both the
185 runtime system and the bytecode for the program. The resulting
186 file is larger, but it can be executed directly, even if the
187 ocamlrun(1) command is not installed. Moreover, the "custom run‐
188 time" mode enables linking OCaml code with user-defined C func‐
189 tions.
190
191 Never use the strip(1) command on executables produced by
192 ocamlc -custom, this would remove the bytecode part of the exe‐
193 cutable.
194
195 Security warning: never set the "setuid" or "setgid" bits on
196 executables produced by ocamlc -custom, this would make them
197 vulnerable to attacks.
198
199 -dllib -llibname
200 Arrange for the C shared library dlllibname.so to be loaded
201 dynamically by the run-time system ocamlrun(1) at program start-
202 up time.
203
204 -dllpath dir
205 Adds the directory dir to the run-time search path for shared C
206 libraries. At link-time, shared libraries are searched in the
207 standard search path (the one corresponding to the -I option).
208 The -dllpath option simply stores dir in the produced executable
209 file, where ocamlrun(1) can find it and use it.
210
211 -for-pack module-path
212 Generate an object file (.cmo file) that can later be included
213 as a sub-module (with the given access path) of a compilation
214 unit constructed with -pack. For instance,
215 ocamlc -for-pack P -c A.ml will generate a.cmo that can later be
216 used with ocamlc -pack -o P.cmo a.cmo. Note: you can still pack
217 a module that was compiled without -for-pack but in this case
218 exceptions will be printed with the wrong names.
219
220 -g Add debugging information while compiling and linking. This
221 option is required in order to be able to debug the program with
222 ocamldebug(1) and to produce stack backtraces when the program
223 terminates on an uncaught exception.
224
225 -i Cause the compiler to print all defined names (with their
226 inferred types or their definitions) when compiling an implemen‐
227 tation (.ml file). No compiled files (.cmo and .cmi files) are
228 produced. This can be useful to check the types inferred by the
229 compiler. Also, since the output follows the syntax of inter‐
230 faces, it can help in writing an explicit interface (.mli file)
231 for a file: just redirect the standard output of the compiler to
232 a .mli file, and edit that file to remove all declarations of
233 unexported names.
234
235 -I directory
236 Add the given directory to the list of directories searched for
237 compiled interface files (.cmi), compiled object code files
238 (.cmo), libraries (.cma), and C libraries specified with
239 -cclib -lxxx . By default, the current directory is searched
240 first, then the standard library directory. Directories added
241 with -I are searched after the current directory, in the order
242 in which they were given on the command line, but before the
243 standard library directory. See also option -nostdlib.
244
245 If the given directory starts with +, it is taken relative to
246 the standard library directory. For instance, -I +compiler-libs
247 adds the subdirectory compiler-libs of the standard library to
248 the search path.
249
250 -impl filename
251 Compile the file filename as an implementation file, even if its
252 extension is not .ml.
253
254 -intf filename
255 Compile the file filename as an interface file, even if its
256 extension is not .mli.
257
258 -intf-suffix string
259 Recognize file names ending with string as interface files
260 (instead of the default .mli).
261
262 -keep-docs
263 Keep documentation strings in generated .cmi files.
264
265 -keep-locs
266 Keep locations in generated .cmi files.
267
268 -labels
269 Labels are not ignored in types, labels may be used in applica‐
270 tions, and labelled parameters can be given in any order. This
271 is the default.
272
273 -linkall
274 Force all modules contained in libraries to be linked in. If
275 this flag is not given, unreferenced modules are not linked in.
276 When building a library (option -a), setting the -linkall option
277 forces all subsequent links of programs involving that library
278 to link all the modules contained in the library. When compil‐
279 ing a module (option -c), setting the -linkall option ensures
280 that this module will always be linked if it is put in a library
281 and this library is linked.
282
283 -make-runtime
284 Build a custom runtime system (in the file specified by option
285 -o) incorporating the C object files and libraries given on the
286 command line. This custom runtime system can be used later to
287 execute bytecode executables produced with the option
288 ocamlc -use-runtime runtime-name.
289
290 -no-alias-deps
291 Do not record dependencies for module aliases.
292
293 -no-app-funct
294 Deactivates the applicative behaviour of functors. With this
295 option, each functor application generates new types in its
296 result and applying the same functor twice to the same argument
297 yields two incompatible structures.
298
299 -noassert
300 Do not compile assertion checks. Note that the special form
301 assert false is always compiled because it is typed specially.
302 This flag has no effect when linking already-compiled files.
303
304 -noautolink
305 When linking .cma libraries, ignore -custom, -cclib and -ccopt
306 options potentially contained in the libraries (if these options
307 were given when building the libraries). This can be useful if
308 a library contains incorrect specifications of C libraries or C
309 options; in this case, during linking, set -noautolink and pass
310 the correct C libraries and options on the command line.
311
312 -nolabels
313 Ignore non-optional labels in types. Labels cannot be used in
314 applications, and parameter order becomes strict.
315
316 -nostdlib
317 Do not automatically add the standard library directory to the
318 list of directories searched for compiled interface files
319 (.cmi), compiled object code files (.cmo), libraries (.cma), and
320 C libraries specified with -cclib -lxxx . See also option -I.
321
322 -o exec-file
323 Specify the name of the output file produced by the linker. The
324 default output name is a.out, in keeping with the Unix tradi‐
325 tion. If the -a option is given, specify the name of the library
326 produced. If the -pack option is given, specify the name of the
327 packed object file produced. If the -output-obj option is
328 given, specify the name of the output file produced. This can
329 also be used when compiling an interface or implementation file,
330 without linking, in which case it sets the name of the cmi or
331 cmo file, and also sets the module name to the file name up to
332 the first dot.
333
334 -opaque
335 Interface file compiled with this option are marked so that
336 other compilation units depending on it will not rely on any
337 implementation details of the compiled implementation. The
338 native compiler will not access the .cmx file of this unit --
339 nor warn if it is absent. This can improve speed of compilation,
340 for both initial and incremental builds, at the expense of per‐
341 formance of the generated code.
342
343 -open module
344 Opens the given module before processing the interface or imple‐
345 mentation files. If several -open options are given, they are
346 processed in order, just as if the statements open! module1;;
347 ... open! moduleN;; were added at the top of each file.
348
349 -output-obj
350 Cause the linker to produce a C object file instead of a byte‐
351 code executable file. This is useful to wrap OCaml code as a C
352 library, callable from any C program. The name of the output
353 object file must be set with the -o option. This option can also
354 be used to produce a C source file (.c extension) or a compiled
355 shared/dynamic library (.so extension).
356
357 -pack Build a bytecode object file (.cmo file) and its associated com‐
358 piled interface (.cmi) that combines the object files given on
359 the command line, making them appear as sub-modules of the out‐
360 put .cmo file. The name of the output .cmo file must be given
361 with the -o option. For instance,
362 ocamlc -pack -o p.cmo a.cmo b.cmo c.cmo generates compiled files
363 p.cmo and p.cmi describing a compilation unit having three sub-
364 modules A, B and C, corresponding to the contents of the object
365 files a.cmo, b.cmo and c.cmo. These contents can be referenced
366 as P.A, P.B and P.C in the remainder of the program.
367
368 -plugin plugin
369 Dynamically load the code of the given plugin (a .cmo, .cma or
370 .cmxs file) in the compiler. The plugin must exist in the same
371 kind of code as the compiler (ocamlc.byte must load bytecode
372 plugins, while ocamlc.opt must load native code plugins), and
373 extension adaptation is done automatically for .cma files (to
374 .cmxs files if the compiler is compiled in native code).
375
376 -pp command
377 Cause the compiler to call the given command as a preprocessor
378 for each source file. The output of command is redirected to an
379 intermediate file, which is compiled. If there are no compila‐
380 tion errors, the intermediate file is deleted afterwards. The
381 name of this file is built from the basename of the source file
382 with the extension .ppi for an interface (.mli) file and .ppo
383 for an implementation (.ml) file.
384
385 -ppx command
386 After parsing, pipe the abstract syntax tree through the pre‐
387 processor command. The module Ast_mapper(3) implements the
388 external interface of a preprocessor.
389
390 -principal
391 Check information path during type-checking, to make sure that
392 all types are derived in a principal way. When using labelled
393 arguments and/or polymorphic methods, this flag is required to
394 ensure future versions of the compiler will be able to infer
395 types correctly, even if internal algorithms change. All pro‐
396 grams accepted in -principal mode are also accepted in the
397 default mode with equivalent types, but different binary signa‐
398 tures, and this may slow down type checking; yet it is a good
399 idea to use it once before publishing source code.
400
401 -rectypes
402 Allow arbitrary recursive types during type-checking. By
403 default, only recursive types where the recursion goes through
404 an object type are supported. Note that once you have created an
405 interface using this flag, you must use it again for all depen‐
406 dencies.
407
408 -runtime-variant suffix
409 Add suffix to the name of the runtime library that will be used
410 by the program. If OCaml was configured with option
411 -with-debug-runtime, then the d suffix is supported and gives a
412 debug version of the runtime.
413
414 -safe-string
415 Enforce the separation between types string and bytes, thereby
416 making strings read-only. This will become the default in a
417 future version of OCaml.
418
419 -short-paths
420 When a type is visible under several module-paths, use the
421 shortest one when printing the type's name in inferred inter‐
422 faces and error and warning messages.
423
424 -strict-sequence
425 Force the left-hand part of each sequence to have type unit.
426
427 -thread
428 Compile or link multithreaded programs, in combination with the
429 system "threads" library described in The OCaml user's manual.
430
431 -unboxed-types
432 When a type is unboxable (i.e. a record with a single argument
433 or a concrete datatype with a single constructor of one argu‐
434 ment) it will be unboxed unless annotated with [@@ocaml.boxed].
435
436 -no-unboxed-types
437 When a type is unboxable it will be boxed unless annotated with
438 [@@ocaml.unboxed]. This is the default.
439
440 -unsafe
441 Turn bound checking off for array and string accesses (the
442 v.(i)ands.[i] constructs). Programs compiled with -unsafe are
443 therefore slightly faster, but unsafe: anything can happen if
444 the program accesses an array or string outside of its bounds.
445
446 -unsafe-string
447 Identify the types string and bytes, thereby making strings
448 writable. For reasons of backward compatibility, this is the
449 default setting for the moment, but this will change in a future
450 version of OCaml.
451
452 -use-runtime runtime-name
453 Generate a bytecode executable file that can be executed on the
454 custom runtime system runtime-name, built earlier with
455 ocamlc -make-runtime runtime-name.
456
457 -v Print the version number of the compiler and the location of the
458 standard library directory, then exit.
459
460 -verbose
461 Print all external commands before they are executed, in partic‐
462 ular invocations of the C compiler and linker in -custom mode.
463 Useful to debug C library problems.
464
465 -vmthread
466 Compile or link multithreaded programs, in combination with the
467 VM-level threads library described in The OCaml user's manual.
468
469 -vnum or -version
470 Print the version number of the compiler in short form (e.g.
471 "3.11.0"), then exit.
472
473 -w warning-list
474 Enable, disable, or mark as fatal the warnings specified by the
475 argument warning-list.
476
477 Each warning can be enabled or disabled, and each warning can be
478 fatalor non-fatal. If a warning is disabled, it isn't displayed
479 and doesn't affect compilation in any way (even if it is fatal).
480 If a warning is enabled, it is displayed normally by the com‐
481 piler whenever the source code triggers it. If it is enabled
482 and fatal, the compiler will also stop with an error after dis‐
483 playing it.
484
485 The warning-list argument is a sequence of warning specifiers,
486 with no separators between them. A warning specifier is one of
487 the following:
488
489 +num Enable warning number num.
490
491 -num Disable warning number num.
492
493 @num Enable and mark as fatal warning number num.
494
495 +num1..num2 Enable all warnings between num1 and num2 (inclu‐
496 sive).
497
498 -num1..num2 Disable all warnings between num1 and num2 (inclu‐
499 sive).
500
501 @num1..num2 Enable and mark as fatal all warnings between num1
502 and num2 (inclusive).
503
504 +letter Enable the set of warnings corresponding to letter.
505 The letter may be uppercase or lowercase.
506
507 -letter Disable the set of warnings corresponding to letter.
508 The letter may be uppercase or lowercase.
509
510 @letter Enable and mark as fatal the set of warnings corre‐
511 sponding to letter. The letter may be uppercase or lowercase.
512
513 uppercase-letter Enable the set of warnings corresponding to
514 uppercase-letter.
515
516 lowercase-letter Disable the set of warnings corresponding to
517 lowercase-letter.
518
519 The warning numbers are as follows.
520
521 1 Suspicious-looking start-of-comment mark.
522
523 2 Suspicious-looking end-of-comment mark.
524
525 3 Deprecated feature.
526
527 4 Fragile pattern matching: matching that will remain com‐
528 plete even if additional constructors are added to one of the
529 variant types matched.
530
531 5 Partially applied function: expression whose result has
532 function type and is ignored.
533
534 6 Label omitted in function application.
535
536 7 Method overridden without using the "method!" keyword
537
538 8 Partial match: missing cases in pattern-matching.
539
540 9 Missing fields in a record pattern.
541
542 10 Expression on the left-hand side of a sequence that doesn't
543 have type unit (and that is not a function, see warning number
544 5).
545
546 11 Redundant case in a pattern matching (unused match case).
547
548 12 Redundant sub-pattern in a pattern-matching.
549
550 13 Override of an instance variable.
551
552 14 Illegal backslash escape in a string constant.
553
554 15 Private method made public implicitly.
555
556 16 Unerasable optional argument.
557
558 17 Undeclared virtual method.
559
560 18 Non-principal type.
561
562 19 Type without principality.
563
564 20 Unused function argument.
565
566 21 Non-returning statement.
567
568 22 Preprocessor warning.
569
570 23 Useless record with clause.
571
572 24 Bad module name: the source file name is not a valid OCaml
573 module name.
574
575 25 Pattern-matching with all clauses guarded.
576
577 26 Suspicious unused variable: unused variable that is bound
578 with let or as, and doesn't start with an underscore (_) charac‐
579 ter.
580
581 27 Innocuous unused variable: unused variable that is not
582 bound with let nor as, and doesn't start with an underscore (_)
583 character.
584
585 28 A pattern contains a constant constructor applied to the
586 underscore (_) pattern.
587
588 29 A non-escaped end-of-line was found in a string constant.
589 This may cause portability problems between Unix and Windows.
590
591 30 Two labels or constructors of the same name are defined in
592 two mutually recursive types.
593
594 31 A module is linked twice in the same executable.
595
596 32 Unused value declaration.
597
598 33 Unused open statement.
599
600 34 Unused type declaration.
601
602 35 Unused for-loop index.
603
604 36 Unused ancestor variable.
605
606 37 Unused constructor.
607
608 38 Unused extension constructor.
609
610 39 Unused rec flag.
611
612 40 Constructor or label name used out of scope.
613
614 41 Ambiguous constructor or label name.
615
616 42 Disambiguated constructor or label name.
617
618 43 Nonoptional label applied as optional.
619
620 44 Open statement shadows an already defined identifier.
621
622 45 Open statement shadows an already defined label or con‐
623 structor.
624
625 46 Error in environment variable.
626
627 47 Illegal attribute payload.
628
629 48 Implicit elimination of optional arguments.
630
631 49 Missing cmi file when looking up module alias.
632
633 50 Unexpected documentation comment.
634
635 59 Assignment on non-mutable value.
636
637 60 Unused module declaration.
638
639 61 Unannotated unboxable type in primitive declaration.
640
641 The letters stand for the following sets of warnings. Any let‐
642 ter not mentioned here corresponds to the empty set.
643
644 A all warnings
645
646 C 1, 2
647
648 D 3
649
650 E 4
651
652 F 5
653
654 K 32, 33, 34, 35, 36, 37, 38, 39
655
656 L 6
657
658 M 7
659
660 P 8
661
662 R 9
663
664 S 10
665
666 U 11, 12
667
668 V 13
669
670 X 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 30
671
672 Y 26
673
674 Z 27
675
676
677 The default setting is
678 -w +a-4-6-7-9-27-29-32..39-41-42-44-45-48-50-60. Note that
679 warnings 5 and 10 are not always triggered, depending on the
680 internals of the type checker.
681
682 -warn-error warning-list
683 Mark as errors the warnings specified in the argument warn‐
684 ing-list. The compiler will stop with an error when one of
685 these warnings is emitted. The warning-list has the same mean‐
686 ing as for the -w option: a + sign (or an uppercase letter)
687 marks the corresponding warnings as fatal, a - sign (or a lower‐
688 case letter) turns them back into non-fatal warnings, and a @
689 sign both enables and marks as fatal the corresponding warnings.
690
691 Note: it is not recommended to use the -warn-error option in
692 production code, because it will almost certainly prevent com‐
693 piling your program with later versions of OCaml when they add
694 new warnings or modify existing warnings.
695
696 The default setting is -warn-error -a+31 (only warning 31 is
697 fatal).
698
699 -warn-help
700 Show the description of all available warning numbers.
701
702 -where Print the location of the standard library, then exit.
703
704 - file Process file as a file name, even if it starts with a dash (-)
705 character.
706
707 -help or --help
708 Display a short usage summary and exit.
709
710
712 ocamlopt(1), ocamlrun(1), ocaml(1).
713 The OCaml user's manual, chapter "Batch compilation".
714
715
716
717 OCAMLC(1)