我正在尝试找出如何使用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 systemOCaml工具对我来说有点神秘,所以欢迎任何指针。
发布于 2010-05-11 16:29:57
ocamlmklib来解救了。一步一步:
$ 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本机和字节版本已经准备好了。顺便说一句,ocamlc -custom是deprecated。
希望揭开神秘面纱。
发布于 2010-05-11 07:22:12
OCaml工具对我来说有点神秘,
我也这样认为。为什么不用omake呢?omake使构建序列变得简单。http://omake.metaprl.org/manual/omake-build.html#@fun272
发布于 2010-05-11 08:31:25
我在OCaml上有一个落后的版本,但是看起来silly.cma有动态链接的sillystubs,所以你可能需要一个
-ccopt -L${PWD} 在最后一行。
而且,我看不出您正在做的任何事情都需要ocamlfind的额外功能,-you还不如直接调用ocamlc。
https://stackoverflow.com/questions/2807021
复制相似问题