Compilation Targets: Native
OCaml can compile to native code, delivering high-performance executables with optimised machine code for production environments.
What is OCaml Native Code?
OCaml native code is generated by ocamlopt, the native-code compiler that produces standalone executables with an integrated runtime and garbage collector. Key characteristics:
- Faster runtime performance than bytecode
- Standalone executables requiring no external runtime
- Cross-module inlining and optimisation
- Production-ready deployments
When to Use Native Code
Use native code (ocamlopt) when you need faster runtime performance in production, want standalone executables, or are deploying to end users.
Use bytecode (ocamlc) for fast iteration during development, CI/testing environments where compilation speed matters, or maximum portability.
The typical OCaml workflow: develop with bytecode for fast compile times, switch to native code for production releases. The same source code compiles to both targets without modification.
Platform Support
The native-code compiler is available only on 64-bit systems (OCaml 5.0+):
- x86-64 (AMD64) - Linux, macOS, Windows
- ARM64 (AArch64) - Linux, macOS (including Apple Silicon)
- RISC-V - Linux
- IBM Z (s390x) - Linux (OCaml 5.1+)
- PowerPC (PPC64) - Linux
All native code platforms use 63-bit integers (with 1 bit reserved for the garbage collector tag).
Windows: Native compilation is supported via MSVC, MinGW, or Cygwin toolchains. See OCaml on Windows for setup details.
If your target platform lacks native code support, the bytecode compiler provides a highly portable fallback.
Learn More
- Dune Manual: Executable Linking Modes - How to specify native, bytecode, or other compilation modes in Dune
- OCaml Manual: Native-code Compilation - Complete ocamlopt reference
- Using the OCaml Compiler Toolchain - Practical compilation guide
- The Compiler Backend: Bytecode and Native code - Deep dive into compilation internals
Help Improve Our Documentation
All OCaml docs are open source. See something that's wrong or unclear? Submit a pull request.