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 -depend ocamldep-args
200 Compute dependencies, as ocamldep would do.
201
202 -dllib -llibname
203 Arrange for the C shared library dlllibname.so to be loaded
204 dynamically by the run-time system ocamlrun(1) at program start-
205 up time.
206
207 -dllpath dir
208 Adds the directory dir to the run-time search path for shared C
209 libraries. At link-time, shared libraries are searched in the
210 standard search path (the one corresponding to the -I option).
211 The -dllpath option simply stores dir in the produced executable
212 file, where ocamlrun(1) can find it and use it.
213
214 -for-pack module-path
215 Generate an object file (.cmo file) that can later be included
216 as a sub-module (with the given access path) of a compilation
217 unit constructed with -pack. For instance,
218 ocamlc -for-pack P -c A.ml will generate a.cmo that can later be
219 used with ocamlc -pack -o P.cmo a.cmo. Note: you can still pack
220 a module that was compiled without -for-pack but in this case
221 exceptions will be printed with the wrong names.
222
223 -g Add debugging information while compiling and linking. This
224 option is required in order to be able to debug the program with
225 ocamldebug(1) and to produce stack backtraces when the program
226 terminates on an uncaught exception.
227
228 -i Cause the compiler to print all defined names (with their
229 inferred types or their definitions) when compiling an implemen‐
230 tation (.ml file). No compiled files (.cmo and .cmi files) are
231 produced. This can be useful to check the types inferred by the
232 compiler. Also, since the output follows the syntax of inter‐
233 faces, it can help in writing an explicit interface (.mli file)
234 for a file: just redirect the standard output of the compiler to
235 a .mli file, and edit that file to remove all declarations of
236 unexported names.
237
238 -I directory
239 Add the given directory to the list of directories searched for
240 compiled interface files (.cmi), compiled object code files
241 (.cmo), libraries (.cma), and C libraries specified with
242 -cclib -lxxx . By default, the current directory is searched
243 first, then the standard library directory. Directories added
244 with -I are searched after the current directory, in the order
245 in which they were given on the command line, but before the
246 standard library directory. See also option -nostdlib.
247
248 If the given directory starts with +, it is taken relative to
249 the standard library directory. For instance, -I +compiler-libs
250 adds the subdirectory compiler-libs of the standard library to
251 the search path.
252
253 -impl filename
254 Compile the file filename as an implementation file, even if its
255 extension is not .ml.
256
257 -intf filename
258 Compile the file filename as an interface file, even if its
259 extension is not .mli.
260
261 -intf-suffix string
262 Recognize file names ending with string as interface files
263 (instead of the default .mli).
264
265 -keep-docs
266 Keep documentation strings in generated .cmi files.
267
268 -keep-locs
269 Keep locations in generated .cmi files.
270
271 -labels
272 Labels are not ignored in types, labels may be used in applica‐
273 tions, and labelled parameters can be given in any order. This
274 is the default.
275
276 -linkall
277 Force all modules contained in libraries to be linked in. If
278 this flag is not given, unreferenced modules are not linked in.
279 When building a library (option -a), setting the -linkall option
280 forces all subsequent links of programs involving that library
281 to link all the modules contained in the library. When compil‐
282 ing a module (option -c), setting the -linkall option ensures
283 that this module will always be linked if it is put in a library
284 and this library is linked.
285
286 -make-runtime
287 Build a custom runtime system (in the file specified by option
288 -o) incorporating the C object files and libraries given on the
289 command line. This custom runtime system can be used later to
290 execute bytecode executables produced with the option
291 ocamlc -use-runtime runtime-name.
292
293 -no-alias-deps
294 Do not record dependencies for module aliases.
295
296 -no-app-funct
297 Deactivates the applicative behaviour of functors. With this
298 option, each functor application generates new types in its
299 result and applying the same functor twice to the same argument
300 yields two incompatible structures.
301
302 -noassert
303 Do not compile assertion checks. Note that the special form
304 assert false is always compiled because it is typed specially.
305 This flag has no effect when linking already-compiled files.
306
307 -noautolink
308 When linking .cma libraries, ignore -custom, -cclib and -ccopt
309 options potentially contained in the libraries (if these options
310 were given when building the libraries). This can be useful if
311 a library contains incorrect specifications of C libraries or C
312 options; in this case, during linking, set -noautolink and pass
313 the correct C libraries and options on the command line.
314
315 -nolabels
316 Ignore non-optional labels in types. Labels cannot be used in
317 applications, and parameter order becomes strict.
318
319 -nostdlib
320 Do not automatically add the standard library directory to the
321 list of directories searched for compiled interface files
322 (.cmi), compiled object code files (.cmo), libraries (.cma), and
323 C libraries specified with -cclib -lxxx . See also option -I.
324
325 -o exec-file
326 Specify the name of the output file produced by the linker. The
327 default output name is a.out, in keeping with the Unix tradi‐
328 tion. If the -a option is given, specify the name of the library
329 produced. If the -pack option is given, specify the name of the
330 packed object file produced. If the -output-obj option is
331 given, specify the name of the output file produced. This can
332 also be used when compiling an interface or implementation file,
333 without linking, in which case it sets the name of the cmi or
334 cmo file, and also sets the module name to the file name up to
335 the first dot.
336
337 -opaque
338 Interface file compiled with this option are marked so that
339 other compilation units depending on it will not rely on any
340 implementation details of the compiled implementation. The
341 native compiler will not access the .cmx file of this unit --
342 nor warn if it is absent. This can improve speed of compilation,
343 for both initial and incremental builds, at the expense of per‐
344 formance of the generated code.
345
346 -open module
347 Opens the given module before processing the interface or imple‐
348 mentation files. If several -open options are given, they are
349 processed in order, just as if the statements open! module1;;
350 ... open! moduleN;; were added at the top of each file.
351
352 -output-obj
353 Cause the linker to produce a C object file instead of a byte‐
354 code executable file. This is useful to wrap OCaml code as a C
355 library, callable from any C program. The name of the output
356 object file must be set with the -o option. This option can also
357 be used to produce a C source file (.c extension) or a compiled
358 shared/dynamic library (.so extension).
359
360 -pack Build a bytecode object file (.cmo file) and its associated com‐
361 piled interface (.cmi) that combines the object files given on
362 the command line, making them appear as sub-modules of the out‐
363 put .cmo file. The name of the output .cmo file must be given
364 with the -o option. For instance,
365 ocamlc -pack -o p.cmo a.cmo b.cmo c.cmo generates compiled files
366 p.cmo and p.cmi describing a compilation unit having three sub-
367 modules A, B and C, corresponding to the contents of the object
368 files a.cmo, b.cmo and c.cmo. These contents can be referenced
369 as P.A, P.B and P.C in the remainder of the program.
370
371 -plugin plugin
372 Dynamically load the code of the given plugin (a .cmo, .cma or
373 .cmxs file) in the compiler. The plugin must exist in the same
374 kind of code as the compiler (ocamlc.byte must load bytecode
375 plugins, while ocamlc.opt must load native code plugins), and
376 extension adaptation is done automatically for .cma files (to
377 .cmxs files if the compiler is compiled in native code).
378
379 -pp command
380 Cause the compiler to call the given command as a preprocessor
381 for each source file. The output of command is redirected to an
382 intermediate file, which is compiled. If there are no compila‐
383 tion errors, the intermediate file is deleted afterwards. The
384 name of this file is built from the basename of the source file
385 with the extension .ppi for an interface (.mli) file and .ppo
386 for an implementation (.ml) file.
387
388 -ppx command
389 After parsing, pipe the abstract syntax tree through the pre‐
390 processor command. The module Ast_mapper(3) implements the
391 external interface of a preprocessor.
392
393 -principal
394 Check information path during type-checking, to make sure that
395 all types are derived in a principal way. When using labelled
396 arguments and/or polymorphic methods, this flag is required to
397 ensure future versions of the compiler will be able to infer
398 types correctly, even if internal algorithms change. All pro‐
399 grams accepted in -principal mode are also accepted in the
400 default mode with equivalent types, but different binary signa‐
401 tures, and this may slow down type checking; yet it is a good
402 idea to use it once before publishing source code.
403
404 -rectypes
405 Allow arbitrary recursive types during type-checking. By
406 default, only recursive types where the recursion goes through
407 an object type are supported. Note that once you have created an
408 interface using this flag, you must use it again for all depen‐
409 dencies.
410
411 -runtime-variant suffix
412 Add suffix to the name of the runtime library that will be used
413 by the program. If OCaml was configured with option
414 -with-debug-runtime, then the d suffix is supported and gives a
415 debug version of the runtime.
416
417 -safe-string
418 Enforce the separation between types string and bytes, thereby
419 making strings read-only. This is the default.
420
421 -short-paths
422 When a type is visible under several module-paths, use the
423 shortest one when printing the type's name in inferred inter‐
424 faces and error and warning messages.
425
426 -strict-sequence
427 Force the left-hand part of each sequence to have type unit.
428
429 -unboxed-types
430 When a type is unboxable (i.e. a record with a single argument
431 or a concrete datatype with a single constructor of one argu‐
432 ment) it will be unboxed unless annotated with [@@ocaml.boxed].
433
434 -no-unboxed-types
435 When a type is unboxable it will be boxed unless annotated with
436 [@@ocaml.unboxed]. This is the default.
437
438 -unsafe
439 Turn bound checking off for array and string accesses (the
440 v.(i)ands.[i] constructs). Programs compiled with -unsafe are
441 therefore slightly faster, but unsafe: anything can happen if
442 the program accesses an array or string outside of its bounds.
443
444 -unsafe-string
445 Identify the types string and bytes, thereby making strings
446 writable. This is intended for compatibility with old source
447 code and should not be used with new software.
448
449 -use-runtime runtime-name
450 Generate a bytecode executable file that can be executed on the
451 custom runtime system runtime-name, built earlier with
452 ocamlc -make-runtime runtime-name.
453
454 -v Print the version number of the compiler and the location of the
455 standard library directory, then exit.
456
457 -verbose
458 Print all external commands before they are executed, in partic‐
459 ular invocations of the C compiler and linker in -custom mode.
460 Useful to debug C library problems.
461
462 -vmthread
463 Compile or link multithreaded programs, in combination with the
464 VM-level threads library described in The OCaml user's manual.
465
466 -vnum or -version
467 Print the version number of the compiler in short form (e.g.
468 "3.11.0"), then exit.
469
470 -w warning-list
471 Enable, disable, or mark as fatal the warnings specified by the
472 argument warning-list.
473
474 Each warning can be enabled or disabled, and each warning can be
475 fatalor non-fatal. If a warning is disabled, it isn't displayed
476 and doesn't affect compilation in any way (even if it is fatal).
477 If a warning is enabled, it is displayed normally by the com‐
478 piler whenever the source code triggers it. If it is enabled
479 and fatal, the compiler will also stop with an error after dis‐
480 playing it.
481
482 The warning-list argument is a sequence of warning specifiers,
483 with no separators between them. A warning specifier is one of
484 the following:
485
486 +num Enable warning number num.
487
488 -num Disable warning number num.
489
490 @num Enable and mark as fatal warning number num.
491
492 +num1..num2 Enable all warnings between num1 and num2 (inclu‐
493 sive).
494
495 -num1..num2 Disable all warnings between num1 and num2 (inclu‐
496 sive).
497
498 @num1..num2 Enable and mark as fatal all warnings between num1
499 and num2 (inclusive).
500
501 +letter Enable the set of warnings corresponding to letter.
502 The letter may be uppercase or lowercase.
503
504 -letter Disable the set of warnings corresponding to letter.
505 The letter may be uppercase or lowercase.
506
507 @letter Enable and mark as fatal the set of warnings corre‐
508 sponding to letter. The letter may be uppercase or lowercase.
509
510 uppercase-letter Enable the set of warnings corresponding to
511 uppercase-letter.
512
513 lowercase-letter Disable the set of warnings corresponding to
514 lowercase-letter.
515
516 The warning numbers are as follows.
517
518 1 Suspicious-looking start-of-comment mark.
519
520 2 Suspicious-looking end-of-comment mark.
521
522 3 Deprecated feature.
523
524 4 Fragile pattern matching: matching that will remain com‐
525 plete even if additional constructors are added to one of the
526 variant types matched.
527
528 5 Partially applied function: expression whose result has
529 function type and is ignored.
530
531 6 Label omitted in function application.
532
533 7 Method overridden without using the "method!" keyword
534
535 8 Partial match: missing cases in pattern-matching.
536
537 9 Missing fields in a record pattern.
538
539 10 Expression on the left-hand side of a sequence that doesn't
540 have type unit (and that is not a function, see warning number
541 5).
542
543 11 Redundant case in a pattern matching (unused match case).
544
545 12 Redundant sub-pattern in a pattern-matching.
546
547 13 Override of an instance variable.
548
549 14 Illegal backslash escape in a string constant.
550
551 15 Private method made public implicitly.
552
553 16 Unerasable optional argument.
554
555 17 Undeclared virtual method.
556
557 18 Non-principal type.
558
559 19 Type without principality.
560
561 20 Unused function argument.
562
563 21 Non-returning statement.
564
565 22 Preprocessor warning.
566
567 23 Useless record with clause.
568
569 24 Bad module name: the source file name is not a valid OCaml
570 module name.
571
572 25 Deprecated: now part of warning 8.
573
574 26 Suspicious unused variable: unused variable that is bound
575 with let or as, and doesn't start with an underscore (_) charac‐
576 ter.
577
578 27 Innocuous unused variable: unused variable that is not
579 bound with let nor as, and doesn't start with an underscore (_)
580 character.
581
582 28 A pattern contains a constant constructor applied to the
583 underscore (_) pattern.
584
585 29 A non-escaped end-of-line was found in a string constant.
586 This may cause portability problems between Unix and Windows.
587
588 30 Two labels or constructors of the same name are defined in
589 two mutually recursive types.
590
591 31 A module is linked twice in the same executable.
592
593 32 Unused value declaration.
594
595 33 Unused open statement.
596
597 34 Unused type declaration.
598
599 35 Unused for-loop index.
600
601 36 Unused ancestor variable.
602
603 37 Unused constructor.
604
605 38 Unused extension constructor.
606
607 39 Unused rec flag.
608
609 40 Constructor or label name used out of scope.
610
611 41 Ambiguous constructor or label name.
612
613 42 Disambiguated constructor or label name.
614
615 43 Nonoptional label applied as optional.
616
617 44 Open statement shadows an already defined identifier.
618
619 45 Open statement shadows an already defined label or con‐
620 structor.
621
622 46 Error in environment variable.
623
624 47 Illegal attribute payload.
625
626 48 Implicit elimination of optional arguments.
627
628 49 Missing cmi file when looking up module alias.
629
630 50 Unexpected documentation comment.
631
632 59 Assignment on non-mutable value.
633
634 60 Unused module declaration.
635
636 61 Unannotated unboxable type in primitive declaration.
637
638 The letters stand for the following sets of warnings. Any let‐
639 ter not mentioned here corresponds to the empty set.
640
641 A all warnings
642
643 C 1, 2
644
645 D 3
646
647 E 4
648
649 F 5
650
651 K 32, 33, 34, 35, 36, 37, 38, 39
652
653 L 6
654
655 M 7
656
657 P 8
658
659 R 9
660
661 S 10
662
663 U 11, 12
664
665 V 13
666
667 X 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 30
668
669 Y 26
670
671 Z 27
672
673
674 The default setting is
675 -w +a-4-6-7-9-27-29-32..42-44-45-48-50-60. Note that warnings
676 5 and 10 are not always triggered, depending on the internals of
677 the type checker.
678
679 -warn-error warning-list
680 Mark as errors the warnings specified in the argument warn‐
681 ing-list. The compiler will stop with an error when one of
682 these warnings is emitted. The warning-list has the same mean‐
683 ing as for the -w option: a + sign (or an uppercase letter)
684 marks the corresponding warnings as fatal, a - sign (or a lower‐
685 case letter) turns them back into non-fatal warnings, and a @
686 sign both enables and marks as fatal the corresponding warnings.
687
688 Note: it is not recommended to use the -warn-error option in
689 production code, because it will almost certainly prevent com‐
690 piling your program with later versions of OCaml when they add
691 new warnings or modify existing warnings.
692
693 The default setting is -warn-error -a+31 (only warning 31 is
694 fatal).
695
696 -warn-help
697 Show the description of all available warning numbers.
698
699 -where Print the location of the standard library, then exit.
700
701 - file Process file as a file name, even if it starts with a dash (-)
702 character.
703
704 -help or --help
705 Display a short usage summary and exit.
706
707
709 ocamlopt(1), ocamlrun(1), ocaml(1).
710 The OCaml user's manual, chapter "Batch compilation".
711
712
713
714 OCAMLC(1)