LinuxCommandLibrary

ocamlopt

Compile OCaml code to optimized native executables

TLDR

Compile a source file

$ ocamlopt -o [path/to/binary] [path/to/source_file.ml]
copy

Compile with debugging enabled
$ ocamlopt -g -o [path/to/binary] [path/to/source_file.ml]
copy

SYNOPSIS

ocamlopt [options] filename...

PARAMETERS

-o file
    Specify the output file name for the executable or library.

-c
    Compile source files into native object files (.cmx) without linking.

-I dir
    Add dir to the list of directories searched for compiled interface (.cmi) and native object (.cmx) files.

-g
    Add debugging information to the compiled code, enabling more effective debugging with tools like gdb.

-S
    Output assembly code (.s file) for each source file instead of machine code.

-unsafe
    Enable faster but potentially unsafe integer arithmetic and array accesses, by disabling bounds checks and overflow checks.

-shared
    Build a shared library (.so or .dll) instead of an executable.

-inline n
    Set inlining aggressiveness; n is a numeric value (higher means more inlining).

-cc command
    Use command as the C compiler for linking and compiling C stubs.

-cclib lib
    Pass the C library lib to the C linker (e.g., -cclib -lfoo to link libfoo.a or libfoo.so).

-ccopt option
    Pass option directly to the C compiler or linker (e.g., -ccopt -O2).

-fPIC
    Generate position-independent code (required for shared libraries on some systems and for certain dynamic linking scenarios).

-v
    Print the version number of the OCaml compiler and exit.

--help
    Display a summary of available command-line options.

DESCRIPTION

ocamlopt is the optimizing native-code compiler for the OCaml programming language. Unlike ocamlc, which compiles OCaml source code into bytecode that runs on the OCaml virtual machine, ocamlopt translates OCaml source files (.ml, .mli) and compiled interface/object files (.cmi, .cmx) directly into machine code. This results in standalone executables or shared libraries that can be run without the OCaml runtime interpreter, often offering significant performance improvements.

It performs various optimizations such as register allocation, common subexpression elimination, and inlining. ocamlopt is typically used for deploying performance-critical OCaml applications and handles linking with C code and C libraries.

CAVEATS

Debugging native code generated by ocamlopt can be more complex than debugging bytecode, often requiring platform-specific tools like gdb.
Compiled executables are platform-specific (e.g., Linux x86-64 executables won't run on Windows ARM).
Building mixed OCaml/C applications requires careful management of C stubs and linking configurations.

COMPILATION PHASES

ocamlopt typically processes OCaml source files (.ml, .mli) into native object files (.cmx) in a compilation phase. Subsequently, these .cmx files (along with any .c files or C libraries) are linked into a final executable or shared library in a separate linking phase.

CMX VS. CMO FILES

While ocamlc produces .cmo (bytecode object) files, ocamlopt produces .cmx (native code object) files. These .cmx files contain machine code and are suitable for linking into native executables, whereas .cmo files are linked into bytecode executables or interpreted by the ocaml toplevel.

HISTORY

The OCaml language, originally known as Caml Light and later Caml, was developed at INRIA (Institut national de recherche en informatique et en automatique) in France, building upon the ML (Meta Language) family. ocamlopt was introduced as a key component of the OCaml distribution to provide an optimizing native-code compiler, addressing the demand for high-performance applications that could not be fully met by the bytecode compiler (ocamlc). Its continuous development has focused on improving performance, supporting new architectures, and integrating advanced compilation techniques.

SEE ALSO

ocamlc(1), ocaml(1), ocamllex(1), ocamlyacc(1)

Copied to clipboard