How to use -thread compiler flag with ocamlbuild?

2019-03-23 08:58发布

问题:

I am using Jane Street's async_core by adding package(async_core) in _tags.

When I use ocamlbuild -use-ocamlfind -I src test/test_airport.native, it gives me the following error:

camlfind ocamlopt -linkpkg -package async_core -package unix -package netclient -package mongo -package xml-light src/airport.cmx test/test_airport.cmx -o test/test_airport.native ocamlfind: Error from package `threads': Missing -thread or -vmthread switch


I googled it and here is what I got http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual039.html

It says:

Programs that use system threads must be linked as follows:

    ocamlc -thread other options unix.cma threads.cma other files

So I changed my ocamlbuild command like this:

ocamlbuild -use-ocamlfind -cflag -thread -I src test/test_airport.native

But the error remains the same. also the actual command that ocamlbuild generated remains the same without -thread.


How can I deal with this?

回答1:

What you want to know is whether there is an ocamlbuild tag (~ feature) to add the -thread argument to the relevant command-lines, instead of hacking it with -cflag in unsatisfying ways. As explained in this blog post, you should use the -documentation option of ocamlbuild:

% ocamlbuild -documentation | grep thread
flag {. byte, link, ocaml, program, thread .} "threads.cma -thread"
flag {. link, native, ocaml, program, thread .} "threads.cmxa -thread"
flag {. doc, ocaml, thread .} "-I +threads"
flag {. compile, ocaml, thread .} "-thread"

So the answer is: add -tag thread to your ocamlbuild invocation line, or just thread in the relevant place in _tags.