# Dune compilation with -output-obj

**URL:** <https://discuss.ocaml.org/t/dune-compilation-with-output-obj/14302>\
**Category:** Ecosystem\
**Tags:** compiler, dune\
**Created:** [March 14, 2024, 3:07pm UTC](https://discuss.ocaml.org/t/dune-compilation-with-output-obj/14302 "2024-03-14T15:07:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xoolive](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xoolive/32/5188_2.png) [@xoolive](https://discuss.ocaml.org/u/xoolive)\
**Post date:** [March 14, 2024, 3:07pm UTC](https://discuss.ocaml.org/t/dune-compilation-with-output-obj/14302/1 "2024-03-14T15:07:22Z")

</div>

Here is my dune file:

```dune
(executable
 (name binding)
 (modules binding)
 (libraries xxx)
 (modes
  (native object))
 ;(ocamlopt_flags (-output-obj))
 (promote
  (until-clean)
  (into .)))

```

This compiles, but doesn’t work later in my process.

The problem is that line

```auto
Running[3]: (cd _build/default && /home/xo/.opam/5.1.1/bin/ocamlopt.opt -w @1..3@5..28@30..39@43@46..47@49..57@61..62@67@69-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -o binding.exe.o -output-complete-obj /home/xo/.opam/5.1.1/lib/xxx/xxx.cmxa .binding.eobjs/native/dune __exe__ Binding.cmx)

```

I need to change the `-output-complete-obj` into `-output-obj` for it to work.  
(I build the .so later in my process)

About my attempts:

- The commented line doesn’t work as the .cmx compilation needs the flag out
- Just in case, the -output-complete-obj version fails on next step because I need a -fPIC version (which I link with libasmrun\_pic.a as a matter of fact) Maybe there’s another workaround considering that…

Also, for reference, my working compilation line I want to reproduce in dune is:

```auto
opam exec -- ocamlfind ocamlopt -package xxx -linkpkg -output-obj -o binding.o binding.ml

```

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [March 14, 2024, 3:54pm UTC](https://discuss.ocaml.org/t/dune-compilation-with-output-obj/14302/2 "2024-03-14T15:54:11Z")

</div>

> [@xoolive](#):
>
> I need to change the `-output-complete-obj` into `-output-obj` for it to work.

Dune does not support `-output-obj` unfortunately. Your only hope is to write a rule by hand to produce what you need, but I never tried myself so am not sure how complicated it is.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![cvine](https://avatars.discourse-cdn.com/v4/letter/c/90db22/32.png) [@cvine](https://discuss.ocaml.org/u/cvine)\
**Post date:** [March 14, 2024, 4:47pm UTC](https://discuss.ocaml.org/t/dune-compilation-with-output-obj/14302/3 "2024-03-14T16:47:38Z")

</div>

This may be nothing to do with your problem (the issues were different from the ones you report), but to get dune’s `(native object)` mode to work in a toy project of my own I had to compile with the `-noautolink` flag which in turn required explicitly linking with libunixnat.a, libthreadnat.a and liblwt\_unix\_stubs.a.

As I say it was a toy project and I didn’t spend much time looking at what the problem was, but I ended with a flags field in the dune file comprising `(flags :standard -noautolink -cclib -lunixnat -cclib -lthreadsnat -cclib -llwt_unix_stubs)`. With that I could get my simple project to compile and run. YMMV.

---

<div class="post-metadata">

**Author:** ![borisd](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/borisd/32/3965_2.png) [@borisd](https://discuss.ocaml.org/u/borisd)\
**Post date:** [March 14, 2024, 8:45pm UTC](https://discuss.ocaml.org/t/dune-compilation-with-output-obj/14302/4 "2024-03-14T20:45:49Z")

</div>

See also [this comment](https://discuss.ocaml.org/t/pthread-is-missing-on-os-x/10348/10).

---

<div class="post-metadata">

**Author:** ![xoolive](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xoolive/32/5188_2.png) [@xoolive](https://discuss.ocaml.org/u/xoolive)\
**Post date:** [March 14, 2024, 10:09pm UTC](https://discuss.ocaml.org/t/dune-compilation-with-output-obj/14302/5 "2024-03-14T22:09:41Z")

</div>

Thank you for the comments. Unfortunately the flags option doesn’t do the trick ☹

I found out how to write a rule and copy-paste my compilation line. Not that difficult, however, I might as well just write a `Makefile` then 🙄 (looks cleaner)

Last option which just worked: add the following line to prevent the error further away. It doesn’t prevent the `-output-complete-obj` but it seems to do the trick to prevent the errors further ahead.

`(link_flags (-cclib -lasmrun_pic))`

Now last twist: this option is only necessary on Linux. I found something with `enabled_if` but that seems to apply on the whole target, not on the option itself (and I don’t want targets with different names according to the OS)
