LinuxCommandLibrary

ocamlc

Compile OCaml source code to bytecode

TLDR

Create a binary from a source file

$ ocamlc [path/to/source_file.ml]
copy

Create a named binary from a source file
$ ocamlc -o [path/to/binary] [path/to/source_file.ml]
copy

Automatically generate a module signature (interface) file
$ ocamlc -i [path/to/source_file.ml]
copy

SYNOPSIS

ocamlc [options] files

PARAMETERS

-c
    Compile only. Output .cmo and .cmi files without linking.

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

-I directory
    Add directory to the list of paths searched for compiled interfaces (.cmi) and compiled modules (.cmo).

-g
    Add debugging information to the compiled code.

-w flags
    Control warning messages. flags can be a combination of + (enable), - (disable), @ (default) followed by warning tags.

-warn-error flags
    Treat specified warnings as errors.

-v
    Print the compiler version and exit.

-help
    Display a short usage summary and exit.

-pp command
    Pipe source files through the specified external command (preprocessor).

-short-paths
    Shorten error message paths by omitting the current directory prefix.

-a
    Build a bytecode library (.cma) from the specified .cmo files.

-linkpkg
    When linking, also link the packages specified (usually handled by build systems like dune).

-pack
    Pack all compiled modules into a single module.

-no-stdlib
    Do not add the standard library to the search path.

-open module
    Open the specified module at the top of each compiled file.

DESCRIPTION

The ocamlc command is the OCaml bytecode compiler, a fundamental tool in the OCaml programming language ecosystem.
It translates OCaml source code (.ml files) and module interfaces (.mli files) into bytecode, which can then be executed by the ocamlrun virtual machine.

Unlike ocamlopt, which generates native machine code, ocamlc produces architecture-independent bytecode. This makes executables compiled with ocamlc highly portable, requiring only the ocamlrun interpreter to be available on the target system.

Its primary functions include compiling individual modules into .cmo (compiled module) and .cmi (compiled interface) files, archiving multiple .cmo files into .cma (bytecode library) files, and linking these compiled units along with the OCaml standard library to produce a final bytecode executable.

Developers typically use ocamlc for rapid prototyping, educational purposes, or when portability across various systems is a higher priority than raw execution speed.

CAVEATS

Bytecode executables generated by ocamlc require the ocamlrun interpreter to be present on the target system for execution.
While highly portable, bytecode performance is generally lower than native code compiled by ocamlopt, making ocamlc less suitable for performance-critical applications.

OUTPUT FILES

When compiling OCaml source code, ocamlc produces several types of files:
.cmi files: Compiled interface files, generated from .mli files or automatically from .ml files. They contain type information for a module.
.cmo files: Compiled module files (bytecode objects), generated from .ml files. They contain the module's compiled code.
.cma files: Bytecode libraries, which are archives of multiple .cmo files.
Executable files: When linking is performed, a final executable is created, which is a wrapper around the bytecode that invokes ocamlrun.

HISTORY

ocamlc is a core component of the OCaml distribution, tracing its lineage back to the earliest versions of the OCaml language (formerly Caml Light and then Objective Caml).

Its development has run in parallel with the language itself, steadily gaining features such as improved warning systems, support for new language constructs, and integration with modern build tools. From the beginning, ocamlc provided the foundational capability for compiling OCaml code into portable bytecode, a critical feature for its adoption and use across diverse computing environments before native code compilation became widespread or highly optimized.

While ocamlopt (for native code) has gained prominence for performance-sensitive applications, ocamlc remains indispensable for situations prioritizing rapid compilation, cross-platform compatibility, or environments where a full native compiler toolchain might be cumbersome.

SEE ALSO

ocamlopt(1), ocamlrun(1), ocaml(1), ocamlbuild(1), ocamldep(1)

Copied to clipboard