Linker errors when using the `threads` library and building an object file (.o)

What is a the right way to configure Dune to build an object file that includes the Batteries library? I have tried the following:

let test () = ()

let _ =
  Callback.register "test" test

with the following dune file:

(executable
 (name test)
 (modes object)
 (libraries batteries))

… but I get the following error:

$ dune build test.exe.o
    ocamlopt test.exe.o (exit 2)
(cd _build/default && /home/user/.opam/4.03.0/bin/ocamlopt.opt -w @a-4-29-40-41-42-44-45-48-58-59-60-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -o test.exe.o -output-complete-obj -I /home/user/.opam/4.03.0/lib/batteries -I /home/user/.opam/4.03.0/lib/bytes -I /home/user/.opam/4.03.0/lib/num -I /home/user/.opam/4.03.0/lib/ocaml/threads /home/user/.opam/4.03.0/lib/ocaml/unix.cmxa /home/user/.opam/4.03.0/lib/ocaml/nums.cmxa /home/user/.opam/4.03.0/lib/ocaml/bigarray.cmxa /home/user/.opam/4.03.0/lib/ocaml/str.cmxa /home/user/.opam/4.03.0/lib/ocaml/threads/threads.cmxa /home/user/.opam/4.03.0/lib/batteries/batteries.cmxa /home/user/.opam/4.03.0/lib/batteries/batteriesThread.cmxa .test.eobjs/test.cmx)
ld: fatal error: cannot mix -r with dynamic object //lib/x86_64-linux-gnu/libpthread.so.0
File "caml_startup", line 1:
Error: Error during linking

What am I doing wrong?

Are you sure this is a Batteries related issue?

it works on OSX, but I do get this warning:

ld: warning: /usr/lib/libpthread.dylib, ignoring unexpected dylib file

This reminds me something. I can’t remember exactly, though; I think there are initialization code with side effects in batterie that bite me once in a similar way. I’ll try to remember.

Best regards

I ran more experiments:

  • I can compile just fine with libraries like uint, re, or unix
  • Including batteries, core, or threads causes the same error at compile time

I have updated the title accordingly

1 Like

Additionally, I get a similar issue if I try to use ctypes.foreign:

dune build test.exe.o
    ocamlopt test.exe.o (exit 2)
(cd _build/default && /home/clement/.opam/4.03.0/bin/ocamlopt.opt -w @a-4-29-40-41-42-44-45-48-58-59-60-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -o test.exe.o -output-complete-obj -I /home/clement/.opam/4.03.0/lib/bytes -I /home/clement/.opam/4.03.0/lib/ctypes -I /home/clement/.opam/4.03.0/lib/integers -I /home/clement/.opam/4.03.0/lib/ocaml/threads /home/clement/.opam/4.03.0/lib/ocaml/unix.cmxa /home/clement/.opam/4.03.0/lib/ocaml/threads/threads.cmxa /home/clement/.opam/4.03.0/lib/ocaml/bigarray.cmxa /home/clement/.opam/4.03.0/lib/integers/integers.cmxa /home/clement/.opam/4.03.0/lib/ctypes/ctypes.cmxa /home/clement/.opam/4.03.0/lib/ctypes/ctypes-foreign-base.cmxa /home/clement/.opam/4.03.0/lib/ctypes/ctypes-foreign-threaded.cmxa .test.eobjs/test.cmx)
ld: fatal error: cannot mix -r with dynamic object //usr/lib/x86_64-linux-gnu/libffi.so
File "caml_startup", line 1:
Error: Error during linking

This seems related to the threads library. I get the following error on my machine by just using threads:

    ocamlopt test.exe.o (exit 2)
[...]
ld: cannot find -lpthread
File "caml_startup", line 1:
Error: Error during linking

I am also having the same problem. Anybody managed to solve it?

Adding the following, helped:

(link_flags (:standard -I /usr/lib/x86_64-linux-gnu))

1 Like

batteries also installs an unthreaded version of the library; maybe you could try linking against that one.
On my computer, there is:
~/.opam/5.1.0/lib/batteries/unthreaded/batteries_unthreaded.a