Compiling C lib and OCaml exe using it, all using ocamlfind

Posted by Magnus on Stack Overflow See other posts from Stack Overflow or by Magnus
Published on 2010-05-10T22:59:07Z Indexed on 2010/05/10 23:04 UTC
Read the original article Hit count: 377

Filed under:
|
|

I'm trying to work out how to use ocamlfind to compile a C library and an OCaml executable using that C library.

I put together a set of rather silly example files.

% 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"

I believe the following is the way to compile up the library:

% ocamlfind ocamlc -c sillystubs.c
% ar rc libsillystubs.a sillystubs.o
% ocamlfind ocamlc -c silly.mli
% ocamlfind ocmalc -a -o silly.cma -ccopt -L${PWD} -cclib -lsillystubs

Now I don't seem to be able to use the created library though:

% 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

The OCaml tools are somewhat mysterious to me, so any pointers would be most welcome.

© Stack Overflow or respective owner

Related posts about ocaml

Related posts about ocamlfind