"Error: Files <same file twice> both define a module named Thread"



Using oasis, I add <**/*>: thread to the end of my _tags file, because otherwise I get this error:

ocamlfind: Error from packagethreads’: Missing -thread or -vmthread switch`

I know that I need threads because I use Core.Command and Core.Command.Spec in one file, but I don’t know anything about the threads package, why it’s used by Command, etc. (That hasn’t been important to me so far; it hasn’t seemed necessary to learn more.)

I just installed a new OCaml 4.05.0 switch, and now I’m getting this mysterious error:

ocaml setup.ml -build
Finished, 0 targets (0 cached) in 00:00:00.
+ /Users/marshall/.opam/4.05.0/bin/ocamlfind ocamlopt -g -thread threads.cmxa -linkpkg -package batteries -package core -package owl -I src src/probutils.cmxa src/wrightfisherPDFs.cmx -o src/wrightfisherPDFs.native
File "_none_", line 1:
Error: Files /Users/marshall/.opam/4.05.0/lib/ocaml/threads/threads.cmxa
       and /Users/marshall/.opam/4.05.0/lib/ocaml/threads/threads.cmxa
       both define a module named Thread

Any suggestions about how to fix this or what I should look for in order to figure out what’s going on? (Or: What other information should I report here?)

I noticed that myocamlbuild.ml contains this block, which seems relevant, but I don’t understand it:

(* The default "thread" tag is not compatible with ocamlfind.
 * Indeed, the default rules add the "threads.cma" or "threads.cmxa"
 * options when using this tag. When using the "-linkpkg" option with
 * ocamlfind, this module will then be added twice on the command line.
 * To solve this, one approach is to add the "-thread" option when using
 * the "threads" package using the previous plugin.
flag ["ocaml"; "pkg_threads"; "compile"] (S[A "-thread"]);
flag ["ocaml"; "pkg_threads"; "doc"] (S[A "-I"; A "+threads"]);
flag ["ocaml"; "pkg_threads"; "link"] (S[A "-thread"]);
flag ["ocaml"; "pkg_threads"; "infer_interface"] (S[A "-thread"]);
flag ["c"; "pkg_threads"; "compile"] (S[A "-thread"]);
flag ["ocaml"; "package(threads)"; "compile"] (S[A "-thread"]);
flag ["ocaml"; "package(threads)"; "doc"] (S[A "-I"; A "+threads"]);
flag ["ocaml"; "package(threads)"; "link"] (S[A "-thread"]);
flag ["ocaml"; "package(threads)"; "infer_interface"] (S[A "-thread"]);
flag ["c"; "package(threads)"; "compile"] (S[A "-thread"]);


I have been finding similar errors on the web, although I have not yet noticed a solution for my particular problem. (Sorry, should have looked first.) For example this one. Still looking.

However, I found that by deleting threads.cmxa after -thread in the ocamlfind command line above, and running that line by hand, I can build the program (wrightfisherPDFs.native). However, I’m not sure how to make oasis, etc. figure this out for me. I have looked at what’s in setup.ml and myocamlbuild.ml. Most of the incantations in those files are beyond my understanding at this point. Still working on the problem.


I remember occasionally running into errors of that sort while using oasis, unfortunately I don’t remember at the moment how to go ahead fixing. Unless you really need to be using oasis for some reason, I would highly recommend using jbuilder as your build system. It’s much faster and the interface is more streamlined (though the S-expression syntax take some getting used to). It avoids the mass of _tags, ocamlfind and ocamlbuild and directly invokes the OCaml compiler (I think), so you’re less likely to run into these sorts of issues.


Thanks @basis. I’m willing to switch if there’s a good reason. So maybe this is it. I’m not afraid of parentheses. I started with oasis because that’s what was suggested on ocaml.org, and it worked. And now I’m just lazy. :slight_smile: