gpt4 book ai didi

ocaml - 使用它编译C lib和OCaml exe,全部使用ocamlfind

转载 作者:行者123 更新时间:2023-12-04 23:10:04 25 4
gpt4 key购买 nike

我正在研究如何使用 ocamlfind使用该 C 库编译 C 库和 OCaml 可执行文件。

我整理了一组相当愚蠢的示例文件。

% cat sillystubs.c
#include <stdio.h>

#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>

value
caml_silly_silly( value unit )
{
CAMLparam1( unit );
printf( "%s\n", __FILE__ );
CAMLreturn( Val_unit );
}

% cat silly.mli
external silly : unit -> unit = "silly_silly"

% cat foo.ml
open Silly
open String

let _ =
print_string "About to call into silly";
silly ();
print_string "Called into silly"

我相信以下是编译库的方法:
% ocamlfind ocamlc -c sillystubs.c
% ar rc libsillystubs.a sillystubs.o
% ocamlfind ocamlc -c silly.mli
% ocamlfind ocamlc -a -o silly.cma -ccopt -L${PWD} -cclib -lsillystubs

现在我似乎无法使用创建的库:
% ocamlfind ocamlc -custom -o foo foo.cmo silly.cma
/usr/bin/ld: cannot find -lsillystubs
collect2: ld returned 1 exit status
File "_none_", line 1, characters 0-1:
Error: Error while building custom runtime system

OCaml 工具对我来说有点神秘,所以任何指针都会受到欢迎。

最佳答案

ocalmklib 来救援。一步步:

$ ocamlc -verbose -c sillystubs.c
$ ocamlmklib -verbose sillystubs.o silly.mli -o silly
$ ocamlc -verbose silly.cma foo.ml -o foo
File "foo.ml", line 1, characters 0-1:
Error: Error while linking foo.cmo:
The external function `silly_silly' is not available

糟糕,你在sillystubs.c 中定义了caml_silly_silly,但在silly.mli 中引用了silly_silly(太傻了:) 修复:
$ cat silly.mli 
external silly : unit -> unit = "caml_silly_silly"
$ ocamlmklib -verbose sillystubs.o silly.mli -o silly
$ ocamlc -custom -verbose silly.cma foo.ml -o foo
/usr/bin/ld: cannot find -lsilly
collect2: ld returned 1 exit status
File "foo.ml", line 1, characters 0-1:
Error: Error while building custom runtime system

还是没有运气?添加 -I.找到需要的库。
$ ocamlc -I . -verbose silly.cma foo.ml -o foo.byte
$ ocamlc -I . -verbose -custom silly.cma foo.ml -o foo.byte.custom
$ ocamlopt -I . -verbose silly.cmxa foo.ml -o foo.native

但是在“真实”设置中,您确实希望使用 ocamlfind 安装愚蠢的库,然后通过 ocamlfind 运行编译将放置所需的命令行选项,并且一切都会自动运行。从头开始,整个过程如下所示:
$ ocamlc -verbose -c sillystubs.c
$ ocamlmklib -verbose sillystubs.o silly.mli -o silly
$ cat META
version="0"
description="quite silly"
archive(byte)="silly.cma"
archive(native)="silly.cmxa"
$ ocamlfind install silly META silly.cm* *.mli *.a *.so
Installed /usr/local/lib/ocaml/3.11.2/silly/silly.a
Installed /usr/local/lib/ocaml/3.11.2/silly/libsilly.a
Installed /usr/local/lib/ocaml/3.11.2/silly/silly.mli
Installed /usr/local/lib/ocaml/3.11.2/silly/silly.cmxa
Installed /usr/local/lib/ocaml/3.11.2/silly/silly.cmi
Installed /usr/local/lib/ocaml/3.11.2/silly/silly.cma
Installed /usr/local/lib/ocaml/3.11.2/silly/META
Installed /usr/local/lib/ocaml/3.11.2/stublibs/dllsilly.so
Installed /usr/local/lib/ocaml/3.11.2/stublibs/dllsilly.so.owner
$ rm *.cm* *.a *.so *.o
$ ocamlfind ocamlopt -linkpkg -package silly foo.ml -o foo.native
$ ocamlfind ocamlc -custom -linkpkg -package silly foo.ml -o foo.byte.custom
$ ocamlfind ocamlc -linkpkg -package silly foo.ml -o foo.byte

native 和字节版本已准备就绪。顺便说一句 ocamlc -customdeprecated .

希望谜底揭开。

关于ocaml - 使用它编译C lib和OCaml exe,全部使用ocamlfind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2807021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com