Dune compilation with -output-obj

Here is my dune file:

 (name binding)
 (modules binding)
 (libraries xxx)
  (native object))
 ;(ocamlopt_flags (-output-obj))
  (into .)))

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

The problem is that line

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:

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

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.


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.

See also this comment.

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

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 :roll_eyes: (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)