Compiling source code into executable programs is a fairly complex libraries, linkers, and assemblers. It’s important to understand how these fit together to help with your day-to-day workflow of developing, debugging, and deploying applications.
OCaml has a strong emphasis on static type safety and rejects source code that doesn’t meet its requirements as early as possible. The compiler does this by running the source code through a series of checks and transformations. Each stage performs its job (e.g., type checking, optimization, or code generation) and discards some information from the previous stage. The final native code output is low-level assembly code that doesn’t know anything about the OCaml modules or objects that the compiler started with.
You don’t have to do all of this manually, of course. The compiler frontends (
ocamlopt) are invoked
via the command line and chain the stages together for you. Sometimes though, you’ll need to
dive into the toolchain to hunt down a bug or investigate a performance problem. This chapter
explains the compiler pipeline in more depth so you understand how to harness the command-line
In this chapter, we’ll cover the following topics:
The compilation pipeline and what each stage represents
Source preprocessing via Camlp4 and the intermediate forms
The type-checking process, including module resolution
The details of the compilation process into executable ...