简体   繁体   English

处理 OCaml 项目内部的 C 代码依赖项

[英]Dealing with C code dependencies inside OCaml projects

I'm using ocamlbuild to build my OCaml project that uses an external C library "cstub".我正在使用 ocamlbuild 构建我的 OCaml 项目,该项目使用外部 C 库“cstub”。 I found a very solution online to include the C library in the compilation process using ocamlbuild in http://l-lang.blogspot.com/2012/08/incorporating-c-code-in-ocaml-project.html (adding the pdep ["link";] "linkdep" (fun param -> [param]); line to myocamlbuild.ml ). I found a very solution online to include the C library in the compilation process using ocamlbuild in http://l-lang.blogspot.com/2012/08/incorporating-c-code-in-ocaml-project.html (adding the pdep ["link";] "linkdep" (fun param -> [param]);myocamlbuild.ml的行)。

However, my C library has an external ASM dependency itself, and the solution above is not able to capture it.但是,我的 C 库本身具有外部 ASM 依赖项,上面的解决方案无法捕获它。 It fails to link with the ASM functions because it is not compiled together with the ASM file.它无法与 ASM 函数链接,因为它没有与 ASM 文件一起编译。 Therefore, my question is: is there a way to tweak the suggestion given above to add the support to the ASM file when compiling the C program?因此,我的问题是:在编译 C 程序时,有没有办法调整上面给出的建议以添加对 ASM 文件的支持? For example, is there an additional parameter to be added to the pdep command to support additional compilation features?例如,是否需要在pdep命令中添加额外的参数来支持额外的编译功能?

Thank you very much in advance!非常感谢您!

The default preamble for all build-related questions.所有与构建相关的问题的默认序言。 Consider using dune if you have that option.如果您有该选项,请考虑使用沙丘 OCamlBuild is retiring so don't expect too much help on it or good modern documentation. OCamlBuild 即将退休,因此不要期望太多帮助或良好的现代文档。 With that said, I understand that it is not always an option and I am myself using ocamlbuild for my main project.话虽如此,我知道这并不总是一种选择,我自己在我的主要项目中使用ocamlbuild

So the pdep rule that you added to your ocamlbuild plugin is taking a parameter, eg,因此,您添加到 ocamlbuild 插件的pdep规则采用了一个参数,例如,

<*.native>: linkdep(toto_c.o)

This toto_c.o (or anything between the parens) is passed as param and you need to translate it to a list of paths, so if your asm file is compiled to foo.o , you can write it astoto_c.o (或括号之间的任何内容)作为param传递,您需要将其转换为路径列表,因此如果您的 asm 文件编译为foo.o ,您可以将其写为

<*.native>: linkdep(toto_c.o,foo.o)

and your pdep rule should be你的pdep规则应该是

pdep ["link"] "linkdep" (String.split_at_char ',')

If you want ocamlbuild to build foo.o from file foo.s , you will also need to write a rule for assembling files, ( ocamlbuild -documentation will show you all rules known to ocamlbuild , I didn't find any rules for the *.s -> *.o pattern).如果您希望 ocamlbuild 从文件foo.s构建foo.o ,您还需要编写一个用于组装文件的规则,( ocamlbuild -documentation将向您显示ocamlbuild已知的所有规则,我没有找到任何规则*.s -> *.o模式)。

Totally untested, but the rule should look something like this,完全未经测试,但规则应该是这样的,

let asm_rule () =
  let deps = ["%.s"] and prod = "%.o" in
  let action env _ =
    let src = env "%.s" and obj = env "%.o" in
    let tags = tags_of_pathname src ++ "compile" in
    Cmd (S [Sh "as"; T tags; P src; A "-o"; Px obj]) in
  rule "asm: s -> o" ~deps ~prod action

Then add it on dispatch in the Before_rules stage, eg,然后在Before_rules阶段的dispatch中添加它,例如,

let () = dispatch (function
  | Before_rules -> 
    asm_rule ()
  | After_rules ->
    pdep ["link"] "linkdep" (String.split_on_char ',')
  | _ -> ())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM