Thanks for your reply. I’m using the foreign_archives stanza because I need CMake in order to compile my library (it’s using external C++ dependencies).
Even when adding the -lasmrun flag, I still got the same error.
Do you have an idea how can I “reproduce” the same behavior of foreign_stubs with the foreign_library ? Or maybe foreign_stubs is usable with an external build system like CMake but I saw nothing on the doc ?
If I understand correctly you have a C++ library, osm, that you build with CMake, and also some C bindings that are currently being built together with the C++ library, so that libbinding.a contains both the C++ library and the bindings. The idea would be to continue to use foreign_archive to build the osm library, but move the bindings to the OCaml library using foreign_stubs, something like:
Thanks a lot for your help, looks like it’s on the good way.
Actually, it broke the CMake build chain.
I’m using a library called “osmium” which has been installed using vcpkg. To build it, I did a CMake file that tells what libraries to link when building. In fact, I was building binding.cpp with this makefile so I had no errors regarding the libraries used in my “osm” library.
I did what you advised. Looks like my original problem has been solved but I now have an other: since binding.cpp is no more build using my CMake file, I guess that gcc can no more know where to find the libraries used in osm.
Here is the error I have:
$ dune build
make project/bin/osm/dllosm.so,project/bin/osm/libosm.a
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/project/project/bin/osm/osm
[ 25%] Linking CXX shared library libosmsh.so
[ 50%] Built target osmsh
[ 75%] Linking CXX static library libosm.a
[100%] Built target osm
gcc project/bin/osm/binding.o (exit 1)
(cd _build/default/project/bin/osm && /usr/bin/gcc -O2 -fno-strict-aliasing -fwrapv -fPIC -g -I /home/user/.opam/4.12.0/lib/ocaml -o binding.o -c osm/binding.cpp)
In file included from osm/binding.cpp:14:
osm/mapping/osm.hpp:13:10: fatal error: osmium/io/any_input.hpp: No such file or directory
13 | #include <osmium/io/any_input.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
So no, here is my current dune file to compile the library:
It looks like the bindings.cpp file is still inside the osm directory and CMake is trying to “build” it. You should take it out of that directory and move it to the parent directory (where the cosm OCaml library lives). The build of the osmium library has nothing to do with that of the bindings, so there is no reason to keep them in the same directory.
It doesn’t compile, and I got this error when compiling with dune build:
dune build
gcc project/bin/osm/binding.o (exit 1)
(cd _build/default/project/bin/osm && /usr/bin/gcc -O2 -fno-strict-aliasing -fwrapv -fPIC -g -I /home/user/.opam/4.12.0/lib/ocaml -o binding.o -c binding.cpp)
In file included from binding.cpp:14:
osm/mapping/osm.hpp:13:10: fatal error: osmium/io/any_input.hpp: No such file or directory
13 | #include <osmium/io/any_input.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
In fact, when compiling osm.cpp on its own, with make, it does works well (everything is well included). Do I have to pass some flags to dune ? What did I miss ?
This is not a problem compiling osm.cpp but binding.cpp. You need to pass the required include directories in the (foreign_stubs ...) using (include_dirs ...), eg
If you do this you will need to change your #include "osm/mapping/osp.hpp" to #include "mapping/osp.hpp" inside binding.cpp because dune will pass -I osm when compiling it.