I'm trying to link some C++/cmake code with some OCaml code. If the C++ side were simple, I'd just add its object file to ocamlopt. If the OCaml side were simple, I'd add its object file to cmake. But they're both complex programs with lots of dependencies.
Currently, I have it working, but it's a bit of a hack:
I run ocamlopt -output-obj
to get the main OCaml object:
add_custom_command( OUTPUT ocaml_main.o DEPENDS ocaml.ml COMMAND ocamlfind ocamlopt -package mylib -linkpkg -output-obj -o ocaml_main.o ocaml.ml )
I run ocamlopt again with -o
and $PATH
set to include a fake gcc
executable. This fake gcc
removes the initial -o ocaml_main.o
argument and all .o
files except for std_exit.o
and prints out the rest.
This output is added to the CMake arguments (using target_link_libraries
).
Is there a cleaner way to do this (ie get all of the OCaml dependencies, recursively, ready for linking)? Using plain ocamlfind query
gets me part of the way, but misses eg the extra linker flags embedded in the cmxa files.
I may not fully grasp your problem but here are some points that could be relevant :
ocamlopt
and ocamlbuild
pick them up as such (and include -lstdc++
in the list of linked libraries)ocamlfind
to the main projectHere is an example - bindings for the hypertable c++ library : https://github.com/ygrek/ocaml-hypertable
All the linking magic is contained in _oasis :
CCopt: -x c++ -O2 -Wno-deprecated -I/opt/hypertable/current/include CClib: -L/opt/hypertable/current/lib -failsafe -lstdc++ -lHypertable -lHyperComm -lHyperDfsBroker -lHyperCommon -lHyperspace -lHyperTools -llog4cpp -lexpat -lboost_thread -lboost_iostreams -lboost_program_options -lboost_system -lsigar-amd64-linux -lz -lcurses -lrrd -lboost_filesystem CSources: hypertable_stubs.c, cxx_wrapped.h
(of course use pkg-config
if the library provides it)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.