OCaml 作为 C 库,你好世界示例

OCaml as C library, hello world example

我希望通过将 OCaml 编译为包含 C 接口的静态或共享库来通过 C++ 调用 OCaml 代码。 This page 似乎解释了如何为 OCaml 创建 C 接口。但是我该怎么做并编译它呢?以及如何获取要加载到我的 C++ 代码中的 .h 文件?

此外,有人可以解释一下是这部分吗:

The OCaml runtime system comprises three main parts: the bytecode interpreter, the memory manager, and a set of C functions that implement the primitive operations. Some bytecode instructions are provided to call these C functions, designated by their offset in a table of functions (the table of primitives).

我想 OCaml 可以编译成本地机器语言。为什么它被编译成字节码并在运行时解释?是一直这样,还是只适用于用 C 接口编译的 OCaml 库?

该页面的大部分内容描述了如何从 OCaml 调用 C。您想要执行相反的操作,如 Advanced Topics: callbacks from C to OCaml 中所述,靠近页面底部。

正如您所说,当您进行本机编译时,不涉及字节码。本机编译器 (ocamlopt) 生成普通对象(Unix 中的 .o)文件和包含 OCaml 元数据的额外文件。

如果您查看 Advanced Example with callbacks,您会看到一个示例,其中主程序在 C 中,调用了 OCaml 中定义的两个函数。事情应该在 C++ 中类似地工作。 (不过,我自己只用 C 做过。)

更新

这是使用 带有回调的高级示例 中的代码的已解决示例。我 运行 在 Ubuntu 18.04.4 (x86_64) 上使用此代码。

OCaml 代码如下所示:

$ cat mod.ml
let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 1)

let format_result n = Printf.sprintf "Result is: %d\n" n

let () = Callback.register "fib" fib
let () = Callback.register "format_result" format_result

编译这段代码并请求一个完整的目标文件:

$ ocamlopt -output-obj -o bigmod.o mod.ml

将 C 代码重命名为 modwrap.cc。 (代码在 OCaml 手册部分给出。)

$ head -6 modwrap.cc
#include <stdio.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>

int fib(int n)

请注意,OCaml 包含文件是根据它们是从 C 还是 C++ 包含的条件化的(如今几乎所有头文件都是如此)。

OCaml 手册部分的主要函数也是有效的 C++;将其重命名为 main.cc:

$ head -7 main.cc
#include <stdio.h>
#include <caml/callback.h>

extern int fib(int n);
extern char * format_result(int n);

int main(int argc, char ** argv)

现在编译并link一切:

$ g++ -c modwrap.cc
$ g++ -o myprog -I $(ocamlopt -where) \
    main.cc modwrap.o bigmod.o $(ocamlopt -where)/libasmrun.a -ldl
$

现在运行程序

$ ./myprog
fib(10) = Result is: 89

没有自动生成头文件。在此示例中,main.ccextern 行本质上是头文件。如果你想要一个头文件,你必须自己写这样的东西。

更新 2

以下是创建包含 OCaml 函数及其包装器的实际静态库的命令。这假设您已经完成上述编译以创建 bigmod.o 和 modwrap.o:

$ cp $(ocamlopt -where)/libasmrun.a libmyoc.a
$ ar r libmyoc.a bigmod.o modwrap.o

现在您可以在您的 C++ 代码中使用此库(由 main.cc 表示):

$ g++ -o myprog -I $(ocamlopt -where) main.cc -L . -lmyoc -ldl
$ ./myprog
fib(10) = Result is: 89

更新 3

(我更新了上面的命令以在 Unbuntu 上工作。)