foreign non-C library

84 Views Asked by At

I want to use a library built as a .a file in my dune project (exposing some needed functions...), but the dune documentation specifies only how to use foreign C code.

How can one use a non-C built library with dune?

What I am currently trying to do (I can't see how to apply the process of the documentation with a non-C generated program):

Part of my dune file:

(foreign_archives ../bin_jasmin/TESTS)

My main program:

module ForeignFuncs = struct
  open Ctypes
  open Foreign

  let test_ADCX =
    foreign "test_ADCX"
      (ocaml_bytes @-> ocaml_bytes @-> uint64_t @-> uint64_t @-> uint8_t
     @-> returning void)
end

p.s.: I already used this library with Ctypes, compiling with ocamlopt and it worked just fine. For those curious, this is about using Jasmin

1

There are 1 best solutions below

0
On

You seem to be using dune, so you should be able to have a link_flags field in your executable stanza. Something like

 (link_flags (-ccopt -Lpath/to/archive -cclib -lmy-library))

You still need a rule to generate the archive of course. My dune foo is not great so what I have done in the past is to use a Makefile (which I am more familiar with) to build the archive and have the Makefile call up dune when building the OCaml executable, which has the archive as a dependency. Note that if using a relative path to the archive, the path in the dune file is relative (by default) to the _build/default directory. Your non-C built library may in turn require its own support library to be linked in and it would have to be prepared using C linkage and calling conventions.