如何获得OCaml的连接标志与C ++编译的CMake链接 [英] How to get OCaml linker flags to link with C++ cmake build
问题描述
我试图用一些OCaml的code链接一些C ++ / cmake的code。如果C ++方很简单,我只是添加了目标文件ocamlopt。如果OCaml的一面很简单,我想它的目标文件添加到CMake的。但他们既复杂的程序,有很多依赖。
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:
-
我跑
ocamlopt -output-OBJ
让主OCaml的对象:
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
)
我 -o
和 $ PATH
设置为包括假<$ C重新运行ocamlopt $ C> GCC 可执行文件。这种假 GCC
删除初始 -o ocaml_main.o
参数,所有的.o 除了
std_exit.o
文件>,并打印出的其余部分。
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.
这输出被添加到CMake的参数(使用 target_link_libraries
)。
This output is added to the CMake arguments (using target_link_libraries
).
有一个更清洁的方式来做到这一点(即让所有的OCaml的依赖关系,递归的,准备连接)?使用普通 ocamlfind查询
让我的方式的一部分,但如错过嵌入在文件CMXA额外的连接标志。
Is there a cleaner way to do this (i.e. get all of the OCaml dependencies, recursively, ready for linking)? Using plain ocamlfind query
gets me part of the way, but misses e.g. 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 :
- 我觉得这是更容易从ocaml的和链接C ++ code比反之亦然启动,可能是因为OCaml中是比较高的水平和C ++建立文物是简单的,所以有较少连接的逻辑来处理
- 链接自己的C ++ code到ocaml的项目是连接C code没有什么区别,只是增加-xc ++到GCC标志,并在.c文件存储code,使
ocamlopt
和ocamlbuild
去接他们这样(和包括-lstdc ++
中的列表链接库) - 链接的第三方C ++ code是比较容易,如果一个人守所有的C ++ code作为独立的库归档和写入OCaml的绑定到,然后通过
ocamlfind <链接的绑定像往常一样OCaml的库/ code>来的主要项目
-
下面是一个例子 - 为Hypertable的C ++库绑定: HTTP://hypertable.forge。 ocamlcore.org/
- I feel that it is easier to start from ocaml and link c++ code in than vice versa, probably because ocaml is more high level and c++ build artifacts are simpler, so there is less linking logic to handle
- Linking your own c++ code into ocaml project is no different to linking c code, just add -xc++ to gcc flags and store code in .c files so that
ocamlopt
andocamlbuild
pick them up as such (and include-lstdc++
in the list of linked libraries) - Linking third party c++ code is easier if one keeps all c++ code as separate library archive and writes OCaml bindings to that, then links those bindings as usual OCaml library via
ocamlfind
to the main project Here is an example - bindings for the hypertable c++ library : http://hypertable.forge.ocamlcore.org/
所有魔法链接中包含_oasis:
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
(当然使用 pkg配置
如果库提供的话)
这篇关于如何获得OCaml的连接标志与C ++编译的CMake链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!