anol
anol

Reputation: 8953

OCaml native executable with linked C library

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.

Upvotes: 3

Views: 85

Answers (1)

J&#252;rgen H&#246;tzel
J&#252;rgen H&#246;tzel

Reputation: 19717

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

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

Upvotes: 4

Related Questions