OCaml作为C库,您好世界示例 [英] OCaml as C library, hello world example

查看:114
本文介绍了OCaml作为C库,您好世界示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过将OCaml编译为包含C接口的静态或共享库来通过C ++调用OCaml代码. 此页面似乎在解释如何为以下内容创建C接口OCaml.但是我该怎么做并进行编译呢?以及如何获取要在我的C ++代码中加载的.h文件?

I wish to call OCaml code through C++ by compiling OCaml to a static or shared library that contains a C interface. This page seems to explain how to create a C interface for OCaml. But how do I do it and compile it? And how do I obtain the .h file to load in my C++ code?

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

Also, could someone explain to be this part:

OCaml运行时系统包括三个主要部分:字节码 解释器,内存管理器和一组C函数 实现原始操作.一些字节码指令是 提供来调用这些C函数,由它们在a中的偏移量指定 函数表(原始表).

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库?

I thougth OCaml could be compiled to native machine language. Why it is compiled to bytecode and interpreted at runtime? Is it always like that, or only for OCaml libraries compiled with C interface?

推荐答案

该页面的大部分内容描述了如何从OCaml调用C.您想做相反的事情,这在

Most of that page describes how to call C from OCaml. You want to do the reverse, which is described in Advanced Topics: callbacks from C to OCaml, closer to the bottom of the page.

正如您所说的那样,当您进行本机编译时,不涉及字节码.本机编译器(ocamlopt)会生成普通对象(在Unix中为.o)文件以及包含OCaml元数据的其他文件.

When you do native compilation there is no bytecode involved, just as you say. The native compiler (ocamlopt) produces ordinary object (.o in Unix) files and extra files containing OCaml metadata.

如果您查看高级带有回调的示例,您将看到一个示例,其中主程序位于C中,并调用了OCaml中定义的两个函数.在C ++中,事情应该类似地工作. (不过,我只是用C自己做的.)

If you look at Advanced Example with callbacks, you'll see an example where the main program is in C, with calls to two functions defined in OCaml. Things should work similarly in C++. (I have only done this in C myself, however.)

更新

以下是使用带有回调的高级示例中的代码编写的示例.我正在Ubuntu 18.04.4(x86_64)上运行此代码.

Here is the worked-out example using the code from Advanced example with callbacks. I am running this code on Ubuntu 18.04.4 (x86_64).

OCaml代码如下:

The OCaml code looks like this:

$ 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

编译此代码并要求完整的目标文件:

Compile this code and ask for a complete object file:

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

将C代码重命名为modwrap.cc. (代码在OCaml手册部分中提供.)

Rename the C code to modwrap.cc. (The code is given in the OCaml manual section.)

$ 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 ++中是有条件的(目前几乎所有头文件都如此).

Note that the OCaml include files are conditionalized as to whether they're being included from C or C++ (as are almost all header files these days).

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

The main function from the OCaml manual section is also valid C++; rename it to 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)

现在编译并链接所有内容:

Now compile and link everything:

$ 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行本质上是头文件.如果想要头文件,则必须自己编写类似的内容.

There is no automatic generation of header files. In this example the extern lines of main.cc are the header file in essence. If you want a header file you'll have to write something like this yourself.

更新2

以下是用于创建包含OCaml函数及其包装程序的实际静态库的命令.假设您已完成上面的编译以创建bigmod.o和modwrap.o:

Here are the commands for creating an actual static library containing the OCaml functions and their wrappers. This assumes that you have done the compiles above to create bigmod.o and modwrap.o:

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

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

Now you can use this library in your C++ code (represented by main.cc):

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

更新3

(我更新了上述命令以在Unbuntu上运行.)

(I updated the above commands to work on Unbuntu.)

这篇关于OCaml作为C库,您好世界示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆