Using external libraries with ocamlbuild

I am trying to use ocamlbuild instead of make , but I cannot properly link my object files to external .cma libraries. It looks like ocamlbuild first tries to determine the dependencies, and then ignores flags like -L/path/to/lib.cma . With make I just passed all the necessary directories with the -I and -L flags to ocamlc , but they don't seem to work with ocamlbuild - ocamlc , it continues to fail because it cannot find the necessary libraries.

+4
source share
1 answer

You have at least 2 ways to pass your parameters to ocamlbuild so that it picks up your library:

  • You can use ocamlbuild command line options:

     $ ocamlbuild -cflags '-I /path/to/mylibrary' -lflags '-I /path/to/mylibrary mylibrary.cma' myprogram.byte 

    Replace .cma with .cmxa for your own executable.

  • Use the myocamlbuild.ml file so that ocamlbuild "knows" about the library and marks the files that 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 command in myocamlbuild.ml tells the tool that a library named "mylibrary" (with specific implementations ending in .cma or .cmxa or others - profiling, plugins) is located in the directory "/ path / to / MyLibrary".

    All files matching the glob pattern in the project directory will be linked using "mylibrary" on ocamlbuild and compiled using special parameters (so you donโ€™t have to worry about the native or byte of the target). Example:

     <src/somefile.ml>: use_mylibrary 

Note: if the library is located in a path known to the compiler (usually /usr/lib/ocaml or /usr/local/lib/ocaml ), then the path prefix can be safely replaced with + , therefore /usr/lib/ocaml/mylibrary becomes +mylibrary >.

+7
source

All Articles