I'm trying to use ocamlbuild
instead of make
, but I'm unable to correctly link my object files with external .cma
libraries. It seems like ocamlbuild
tries to determine dependencies first and then ignores flags like -L/path/to/lib.cma
. With make
I was just passing all necessary directories with -I
and -L
flags to ocamlc
, but these don't seem to work with ocamlbuild
-- ocamlc
keeps failing because it can't find necessary libraries.
You have at least 2 ways to pass your parameters to ocamlbuild for it to take your library in account:
You can use the command line parameters of ocamlbuild:
$ ocamlbuild -cflags '-I /path/to/mylibrary' -lflags '-I /path/to/mylibrary mylibrary.cma' myprogram.byte
Replace .cma
by .cmxa
for a native executable.
Use the myocamlbuild.ml file to let ocamlbuild "know" about the library, and tag the files which need it in the _tag file:
In myocamlbuild.ml
:
open Ocamlbuild_plugin
open Command
dispatch begin function
| After_rules ->
ocaml_lib ~extern:true ~dir:"/path/to/mylibrary" "mylibrary"
| _ -> ()
In _tags
:
<glob pattern>: use_mylibrary
The ocaml_lib
instruction in myocamlbuild.ml
tells the tool that the library named "mylibrary" (with specific implementations ending in .cma
or .cmxa
or others - profiling, plugins) is located in the directory "/path/to/mylibrary".
All the files corresponding to glob pattern
in the project directory will be associated to the use of "mylibrary" by ocamlbuild
, and compiled with the ad hoc parameters (so you don't need to worry about native or byte targets).
Ex:
<src/somefile.ml>: use_mylibrary
Note: if the library is located in the path known by the compiler (usually /usr/lib/ocaml
or /usr/local/lib/ocaml
), then the path prefix can be safely replaced by a +
sign, so /usr/lib/ocaml/mylibrary
becomes +mylibrary
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With