c++ - 使用 ocamlbuild 构建 C++ 代码

标签 c++ ocaml ocamlbuild

我找到了许多关于如何使用 ocamlbuild 从 C 源代码构建 .o 文件的指南。但是,这些不适用于 C++ 文件,ocamlbuild 无法开箱即用。

我已经尝试编写一个 myocamlbuild.ml 文件(请求后如下所示)提供从 .cpp.o 的规则,并且ocamlc 失败,提示它不知道如何处理 .cpp 文件(即使编译器通过命令行设置为 g++标志)。

open Ocamlbuild_plugin ;;

let ext_obj = !Options.ext_obj;;
let x_o = "%"-.-ext_obj;;

rule "ocaml C++ stubs: cpp -> o"
  ~prod:x_o
  ~dep:"%.cpp"
  begin fun env _build ->
    let c = env "%.cpp" in
    let o = env x_o in
    let comp = 
      if Tags.mem "native" (tags_of_pathname c) then !Options.ocamlopt else !Options.ocamlc in
    let cc = 
      Cmd(S[comp; T(tags_of_pathname c++"c"++"compile"); A"-custom"; A"-cc"; A"g++"; A"-c"; Px c]) in
    if Pathname.dirname o = Pathname.current_dir_name then cc
    else Seq[cc; mv (Pathname.basename o) o]
  end;;

文件libsoundness.clib由一堆.o文件组成。

当我执行 ocamlbuild libsoundness.a 时,我得到以下输出:

Finished, 0 targets (0 cached) in 00:00:00.
+ /usr/bin/ocamlc.opt -custom -cc g++ -c src/soundness/proof_aut.cpp
/usr/bin/ocamlc.opt: don't know what to do with src/soundness/proof_aut.cpp.
Usage: ocamlc <options> <files>
<snipped long list of ocamlc options>

C++ 的唯一其他解决方案似乎是 ocamlbuild-ctools,其网站已失效(我无法下载任何源代码)。

有什么想法吗?

最佳答案

比照。 my answer to the similar question .

我推荐以下方法:

  1. .c命名c++文件扩展名 ocamlc接他们
  2. 告诉gcc这些实际上是带有 -x c++ 的 C++ 文件
  3. 告诉ocamlbuild该构建实际上取决于 .h文件(例如,在 .h.c 字段中将 CSources 文件与 _oasis 一起列出)
  4. 不要忘记链接 -lstdc++明确原因gcc作为 C 编译器,它不会那样做
  5. 使用cxx_wrapped.h获得真正的 C++ 感觉
  6. 利润!!

参见 hypertable bindings作为这种方法的一个例子。

关于c++ - 使用 ocamlbuild 构建 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21728826/

相关文章:

从函数调用时c++ getline编译错误

ocaml - oasis 选择了错误的 ocamlbuild

ocaml - ocamlgraph 中的编译错误

c++ - 在 makefile 的子目录中链接对象构建

c++ - 如何从四面体网格中提取表面三角形?

c++ - 具有多个模板参数的类模板

expression - 奥卡姆 : multiply expression (*) int int -> int

casting - OCaml:将 bool 转换/转换为 int

OCaml:如何解决多个 `cmi` s 的 findlib 警告

ocaml - OCaml 中 Array.create 的奇怪之处