Using external libraries with ocamlbuild
Asked Answered
W

1

7

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.

Watercolor answered 26/1, 2013 at 18:4 Comment(0)
S
9

You have at least 2 ways to pass your parameters to ocamlbuild for it to take your library in account:

  1. 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.

  2. 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.

Statics answered 26/1, 2013 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.