简体   繁体   中英

Using external libraries with ocamlbuild

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:

  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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM