English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
problem creating .cma library
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-12-30 (19:13)
From: rouanvd@s...
Subject: problem creating .cma library
Hi everyone.

I am trying to create an ocaml binding to a C library as a test to see if I
can get the ocaml FFI to work.  This is just experimental so I can gain
experience to later write bindings to real C libraries for ocaml.

I am using ocaml 3.11.0 with the MingW toolchain on windows (vista).

I first create a dll called libmylib.dll which contains 1 method called:
void my_print(char* str);

The DLL is created with the MingW toolchan and works correctly as I create
a test C application that links against libmylib.dll and executes correctly.

the library is created with the following gcc commands:

 gcc -fPIC -Wall -c mylib.c
 gcc -shared -o libmylib.dll mylib.o

Now I create the mylib_stub.c file which contains the marshalling code
between the libmylib.dll library and ocaml.  This file contains the
following C code:

#include <stdio.h>
#include <caml/mlvalues.h>
#include "../mylib/mylib.h"

CAMLprim value
my_print_stub(value v) {
    char* str = (char*)String_val( v );
    my_print( str );

    return Val_unit;

I then create the mylib.ml file which contains the following ocaml code:

external my_print : string -> unit = "my_print_stub"

I then use the following commands to create a .cma file that is supposed
to expose the my_print function in the libmylib.dll to ocaml:

 ocamlc -c mylib.ml
 ocamlc -c mylib_stub.c

 ocamlmklib -o _stubs mylib_stub.o -L/mingw/lib -L. -lmylib

 ocamlc -a -o mylib.cma mylib.cmo -dllib -l_stubs -ccopt -L. -cclib -lmylib

Up to this point, there are no errors.

When I finaly build the test ocaml app which is supposed to use the
mylib.cma library, ocamlc tells me:

 "Unbound module Mylib"

The command used to compile the test app is

 ocamlc -verbose mylib.cma -c Main.ml

The Main.ml file contains the following code:

open Mylib

let main () =
    my_print "my_print = ok"

main ()

Is there a way to verify that the .cma library was created correctly?

I had to install flexlink for ocaml to get the dll linking to work on

Kind regards