使用ocamlbuild构建C ++代码(Building C++ code with ocamlbuild)

编程入门 行业动态 更新时间:2024-10-28 15:29:22
使用ocamlbuild构建C ++代码(Building C++ code with ocamlbuild)

我找到了许多关于如何使用ocamlbuild从C源构建.o文件的ocamlbuild 。 但是,这些不适用于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其网站已不存在(我无法下载任何来源)。

有任何想法吗?

I have found many guides on how to build .o files from C sources using ocamlbuild. These do not apply to C++ files, however, which ocamlbuild cannot build out of the box.

I have tried writing a myocamlbuild.ml file (shown below after request) providing a rule from .cpp to .o and that fails with ocamlc complaining that it does not know what to do with a .cpp file (even when the compiler is set to g++ via command line flags).

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;;
 

The file libsoundness.clib consists of a bunch of .o files.

When I do ocamlbuild libsoundness.a I get the following output:

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>
 

The only other solution for C++ seems to be ocamlbuild-ctools whose website is defunct (I could not download any sources).

Any ideas?

最满意答案

比照 我对类似问题的回答 。

我推荐以下方法:

命名扩展名为.c c ++文件,以便ocamlc选择它们 告诉gcc这些实际上是带有-x c++ c ++文件 告诉ocamlbuild构建实际上依赖于.h文件(例如,在CSources字段中列出.h文件和.c文件) 不要忘记链接-lstdc++明确地导致gcc不会那样做C编译器 使用cxx_wrapped.h来获得真正的C ++感觉 利润!!

请参阅超级绑定作为此方法的示例。

cf. my answer to the similar question.

I recommend the following approach :

name c++ files with .c extension so that ocamlc picks them up tell gcc that those are actually c++ files with -x c++ tell ocamlbuild that the build actually depends on .h files (e.g. list .h files together with .c in the CSources field in _oasis) do not forget to link -lstdc++ explicitely cause gcc will not do that being a C compiler it is use cxx_wrapped.h to get that real C++ feel PROFIT!!

See hypertable bindings as an example of this approach.

更多推荐

本文发布于:2023-07-30 03:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1322192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   ocamlbuild   code   Building

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!