Dune with external Ocaml library

I would like to try building my Ocaml program with dune, instead of a Makefile. It will need to be linked with a library of Ocaml modules.

That seems like a job for (libraries …), but this library isn’t “installed” in any meaningful sense, it’s just something like /home/src/xx.cmxa, so naturally “xx” can’t be found - and apparently neither can “/home/src/xx.cmxa” .

How do I point dune to this file?

Dune can use any library that is installed according to Findlib conventions (including the presence of META file). It suffices to set the OCAMLPATH variable to point to the directory containing the Findlib packages.

If your library does not have Findlib metadata (ie a META file), I’m afraid you are out of luck, but adding such metadata is typically easy.

Cheers,
Nicolas

That’s interesting, thanks for that information.

To be clear, though, I don’t really want dune to find the library. I have found it, I just want to link the library I choose to specify. This afternoon it may be another version of the same library. It looks like I had better stay with the Makefile for this project.

You can use Dune “as” make, i.e. specify the details for the compiler. I don’t have a solution at the fingertips, but something like OCaml Flags — Dune documentation

If Dune is able to find the library (eg by setting OCAMLPATH and providing the required metadata), then it can link with it (by adding its name to the (libraries) field).

Dune is an opinionated build system, which in practice means that it proposes a way to achieve an objective, but does not necessarily cater to every other use case. In this specific situation, Dune does not make it easy to link with “naked” .cma or .cmxa archives, and instead requires Findlib metadata for every library. If you are not in a situation where you can provide such metadata, then indeed maybe Dune is not the best solution for your use case.

Cheers,
Nicolas

Sure. The only clarification I need to insist on there is that the situation is not really that I can’t provide the metadata, but that I don’t want to use it. The build system shouldn’t go looking for the file. I will specify it.

You may consider that a “use case”, or not, I don’t know, but it seems that dune’s opinion is that this is outside its domain.

Dune is a source-based build system, yeah. If you can give it the source code of your library, it will happily build whatever you want.

I’m wondering, can we hardcode external library in the specific ocamlopt_flags?