具有链接C库的OCaml本机可执行文件

时间:2015-06-16 15:37:04

标签: ocaml

从一组OCaml和C源生成本机OCaml可执行文件的最简单的命令行是什么,这些源使用需要通过-l<lib>包含的C库,例如-lm

例如,下面的代码说明了一个(人为的)示例:

// test.c
#include <math.h>
#include <caml/alloc.h>
value my_sqrt(value x) { return copy_double(sqrt(Double_val(x))); }
// test.ml
external sqrt_c: float -> float = "my_sqrt"

let () =
  Printf.printf "srqt(5) = %g\n" (sqrt_c 5.0)

在此示例中,ocamlc -o next -custom test.c test.ml -cclib -lm生成OCaml字节码,但-custom无法使用ocamlopt

1 个答案:

答案 0 :(得分:3)

您必须为C-File使用不同的文件名:

   ocamlopt  test-native.c test.ml -cclib -lm -o test