使用ocamlbuild与外部库

7
我想使用ocamlbuild而不是make,但我无法正确地将我的对象文件与外部的.cma库链接起来。似乎ocamlbuild首先尝试确定依赖项,然后忽略像-L/path/to/lib.cma这样的标志。使用make时,我只需通过-I-L标志向ocamlc传递所有必要的目录,但这些在ocamlbuild中似乎不起作用--ocamlc因找不到必要的库而一直失败。
1个回答

9

您至少有两种方法将参数传递给ocamlbuild,以使其考虑您的库:

  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
    
注意:如果库位于编译器所知的路径中(通常为/usr/lib/ocaml/usr/local/lib/ocaml),则路径前缀可以安全地替换为+号,因此/usr/lib/ocaml/mylibrary就变成了+mylibrary

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接