C ++ code generation with ocamlbuild

I found many tutorials on how to create files .ofrom C sources using ocamlbuild. However, this does not apply to C ++ files; however, ocamlbuildit cannot be built out of the box.

I tried to write the file myocamlbuild.ml(shown below after the request), providing a rule from .cppto .o, and this fails with ocamlccomplaining that it does not know what to do with the .cppfile (even if the compiler is installed on 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;;

A file libsoundness.clibconsists of a group of .ofiles.

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>

++ ocamlbuild-ctools, - ( - ).

?

+4
1

. .

:

  • ++ .c, ocamlc
  • gcc, ++ -x c++
  • ocamlbuild, .h (, .h .c CSources _oasis)
  • -lstdc++ gcc C,
  • cxx_wrapped.h, ++.
  • PROFIT!!

. hypertable bindings.

+6

Source: https://habr.com/ru/post/1526545/


All Articles