Linking with different C library depending on OS

I’m porting the ocamlfuse package to dune and it required linking with (c_library_flags (-losxfuse)) on Mac and (c_library_flags (-lfuse)) on all other operating systems. How do I go about customizing the dune build based on host operating system?

Do I have to use dune “environments” for this? I imagine I would also need to modify the opam file to use different build commands based on OS?

The recipe for this is as follows:

  1. Create an OCaml script using the dune.configurator library that will do the OS detection and output a flags file in sexp format

  2. Use (:include flags.sexp) where flags.sexp is generated by your script.

Refer to this manual page for more information: and if you have any more questions then please ask.

There are no dune “environments”, I think you might mean profiles here but I wouldn’t recommend them for this use case. Conditional compilation solves this problem. I also advise against using anything but the standard dune command in the opam file. All of this configuration should be done in your dune build itself.


Thank you Rudi, this was extremely helpful.

For posterity:

ocamlfuse packages is now ported, and you can see the relevant dune file and file.

The project also uses camlidl to generate the C bindings, which may be of interest.

When I said “environments”, I was referring to the env stanza which came up when I searched the docs for “environment”, the keyword “operating system” did not yield anything useful, the keyword I needed was “configuration”, but that never occurred to me.

1 Like