LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

ocamlopt

OCaml native-code compiler

TLDR

Compile to native code
$ ocamlopt -o [program] [source.ml]
copy
Compile multiple files
$ ocamlopt -o [program] [file1.ml] [file2.ml]
copy
Compile with optimization
$ ocamlopt -O3 -o [program] [source.ml]
copy
Compile to object file
$ ocamlopt -c [source.ml]
copy
Create native library
$ ocamlopt -a -o [library.cmxa] [file1.ml] [file2.ml]
copy

SYNOPSIS

ocamlopt [options] files...

DESCRIPTION

ocamlopt is the OCaml native-code compiler. It compiles OCaml source files directly to native machine code, producing significantly faster executables than the bytecode compiler ocamlc. The output is architecture-specific and not portable across platforms.Compilation proceeds in three phases: type-checking and compilation to `.cmx` / `.o` files (`-c`), optional library archiving (`-a`), and linking. Interface files (`.mli`) are compiled to `.cmi` files shared with the bytecode compiler.

PARAMETERS

-o file

Specify the output filename.
-c
Compile only; suppress the linking phase. Produces `.cmx` and `.o` files.
-a
Build a library (`.cmxa` and `.a` files) from the given object files.
-shared
Build a dynamically loadable plugin (`.cmxs` file).
-pack
Combine multiple `.cmx` files into a single compilation unit.
-O level
Optimization level: `0` disables all optimizations, `3` enables aggressive optimization including unboxing. Default is `1`.
-I dir
Add dir to the search path for `.cmi` and `.cmx` files.
-g
Add debugging information to enable stack backtraces at runtime.
-S
Keep the assembly code produced during compilation in a `.s` file.
-inline n
Set the aggressiveness of inlining (default 10; higher means more inlining).
-compact
Optimize the produced code for space rather than speed.
-unsafe
Disable bounds checking on array/string accesses and zero-division checks; faster but unsafe.
-for-pack module-path
Generate an object file that can later be included as a sub-module in a `-pack` compilation.
-linkall
Force all modules contained in libraries to be linked in.
-pp command
Pipe source files through the given preprocessor command.
-ppx command
Pipe the abstract syntax tree through the given preprocessor.
-w warning-list
Enable, disable, or mark as fatal specific compiler warnings.
-warn-error warning-list
Turn the specified warnings into errors.
-open Module
Open the named module before processing each source file.
-nostdlib
Do not include the standard library directory in the search path.
-cc ccomp
Use ccomp as the C linker when building executables.
-cclib -llib
Link with the given C library.
-ccopt option
Pass option to the C compiler and linker.
-verbose
Print all external commands as they are executed.
-v
Print the version number of the compiler and the location of the standard library.
-i
Print inferred interface to stdout; do not produce object files.
-bin-annot
Generate detailed type and location information in `.cmt` and `.cmti` files.

CAVEATS

Native-code compilation is slower than bytecode compilation. The resulting executables are platform-specific. Debugging native code is harder than debugging bytecode; ocamldebug does not support native executables (use `-g` and system debuggers such as `gdb`).

HISTORY

The OCaml native-code compiler was developed at INRIA alongside the bytecode compiler as part of the Caml/OCaml project to provide high-performance compilation while retaining the language's type safety guarantees.

SEE ALSO

ocamlc(1), ocamlfind(1), opam(1), dune(1)

Copied to clipboard
Kai