OCaml native executable with linked C library

95 Views Asked by At

What's the simplest command line to produce a native OCaml executable from a set of OCaml and C sources which use a C library that needs to be included via -l<lib>, such as -lm?

For instance, the following code illustrates a (contrived) example where it would be necessary:

// 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)

In this example, ocamlc -o next -custom test.c test.ml -cclib -lm produces OCaml bytecode, but -custom is not available for ocamlopt.

1

There are 1 best solutions below

0
On BEST ANSWER

You have to use a different filename for your C-File:

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