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

**URL:** <https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231>\
**Category:** Ecosystem\
**Tags:** build, dune\
**Created:** [January 25, 2019, 4:27pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231 "2019-01-25T16:27:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpitclaudel](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@cpitclaudel](https://discuss.ocaml.org/u/cpitclaudel)\
**Post date:** [January 25, 2019, 4:27pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/1 "2019-01-25T16:27:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anon72795300](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@anon72795300](https://discuss.ocaml.org/u/anon72795300)\
**Post date:** [January 26, 2019, 4:12pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/2 "2019-01-26T16:12:21Z")

</div>

Are you sure this is a Batteries related issue?

---

<div class="post-metadata">

**Author:** ![rgrinberg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rgrinberg/32/40_2.png) [@rgrinberg](https://discuss.ocaml.org/u/rgrinberg)\
**Post date:** [January 26, 2019, 8:05pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/3 "2019-01-26T20:05:29Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![ttamttam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ttamttam/32/132_2.png) [@ttamttam](https://discuss.ocaml.org/u/ttamttam)\
**Post date:** [January 26, 2019, 10:47pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/4 "2019-01-26T22:47:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cpitclaudel](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@cpitclaudel](https://discuss.ocaml.org/u/cpitclaudel)\
**Post date:** [January 26, 2019, 11:54pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/5 "2019-01-26T23:54:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cpitclaudel](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@cpitclaudel](https://discuss.ocaml.org/u/cpitclaudel)\
**Post date:** [January 28, 2019, 9:02pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/6 "2019-01-28T21:02:45Z")

</div>

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

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![jeremiedimino](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jeremiedimino/32/699_2.png) [@jeremiedimino](https://discuss.ocaml.org/u/jeremiedimino)\
**Post date:** [January 29, 2019, 8:01am UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/7 "2019-01-29T08:01:02Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![vzaliva](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vzaliva/32/206_2.png) [@vzaliva](https://discuss.ocaml.org/u/vzaliva)\
**Post date:** [March 11, 2019, 4:29pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/8 "2019-03-11T16:29:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vzaliva](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vzaliva/32/206_2.png) [@vzaliva](https://discuss.ocaml.org/u/vzaliva)\
**Post date:** [March 11, 2019, 5:34pm UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/9 "2019-03-11T17:34:18Z")

</div>

Adding the following, helped:

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

---

<div class="post-metadata">

**Author:** ![UnixJunkie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/unixjunkie/32/638_2.png) [@UnixJunkie](https://discuss.ocaml.org/u/UnixJunkie)\
**Post date:** [February 2, 2024, 2:07am UTC](https://discuss.ocaml.org/t/linker-errors-when-using-the-threads-library-and-building-an-object-file-o/3231/10 "2024-02-02T02:07:22Z")

</div>

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
