ocamlfind
Find and link OCaml packages
TLDR
Compile a source file to a native binary and link with packages
Compile a source file to a bytecode binary and link with packages
Cross-compile for a different platform
SYNOPSIS
ocamlfind <command> [options] [arguments]
PARAMETERS
query <package> [...]
Queries information about specified packages, such as compilation flags or paths. Often used with predicates.
list
Lists all currently installed OCaml packages known to ocamlfind.
install <package_name> <files>...
Installs a new OCaml package into the ocamlfind library directory. Requires administrative privileges for system-wide installation.
remove <package_name>
Removes an installed OCaml package.
printconf <variable>
Prints the value of a configuration variable, such as the path where libraries are searched or the version of OCaml.
rpath <path>
Resolves a given path by substituting ocamlfind variables (e.g., $ocamlfind_prefix).
lint <package>
Checks an installed package for common issues and best practices.
help [<command>]
Displays general help or detailed help for a specific ocamlfind command.
DESCRIPTION
ocamlfind is a powerful and indispensable utility in the OCaml programming language ecosystem. Its primary purpose is to simplify the discovery and management of OCaml libraries and packages during compilation and linking. Rather than requiring developers to specify absolute file paths for every library, ocamlfind allows referring to them by their logical package names (e.g., unix, threads, str).
It works by maintaining a database of installed OCaml packages, typically located in directories defined by the OCAMLPATH environment variable or the default system library path. When invoked, ocamlfind can query this database to provide the correct compiler flags (e.g., -I, -cclib) or file paths (.cmi, .cmo, .cmx, .a) needed by ocamlc, ocamlopt, and other OCaml tools.
Beyond simple path resolution, ocamlfind supports complex queries using predicates, allowing users to select specific variants of a library (e.g., native code, bytecode, syntax extensions). It also includes subcommands for installing, removing, listing, and linting packages, making it a comprehensive tool for library management in OCaml projects. Although package managers like Opam often handle the initial installation of libraries, ocamlfind remains the foundational layer for OCaml compilers to locate and utilize those libraries.
CAVEATS
ocamlfind primarily manages packages installed in a specific directory structure. It might not automatically discover packages installed by other means if they are not correctly registered or placed in ocamlfind's search paths.
Modifying system-wide ocamlfind libraries (e.g., using install or remove without a custom OCAMLPATH) often requires root privileges, which should be used with caution.
Managing packages with ocamlfind directly can be complex for large projects. Modern OCaml development often relies on build systems like Dune and package managers like Opam, which abstract away many direct ocamlfind invocations.
PREDICATES
Predicates are keywords used with the query command to specify attributes or variants of a package that ocamlfind should consider. Examples include byte (for bytecode libraries), native (for native code libraries), syntax (for syntax extensions), ppx (for PPX preprocessors), and plugin (for compiler plugins). They allow ocamlfind to select the correct files or compilation flags based on the context.
OCAMLPATH ENVIRONMENT VARIABLE
The OCAMLPATH environment variable is crucial for ocamlfind. It specifies a colon-separated list of directories where ocamlfind should search for OCaml libraries and packages. If not set, ocamlfind defaults to system-wide paths. Users can set this variable to manage custom or project-specific library locations without needing root access.
HISTORY
ocamlfind was created by Gerd Stolpmann and first released in the early 2000s. It quickly became the de-facto standard for managing OCaml libraries due to its ability to abstract away physical library paths and provide a consistent interface for developers. Before the widespread adoption of package managers like Opam, ocamlfind was often used directly for installing and configuring OCaml libraries. While Opam now handles the installation of packages, ocamlfind remains a fundamental underlying component, as Opam installs packages in a way that ocamlfind can discover and use. Its stable design has ensured its continued relevance in the OCaml ecosystem.