Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] How to link libraries with C parts (Bug?)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-06-08 (19:46)
From: Nils Goesche <cartan@c...>
Subject: [Caml-list] How to link libraries with C parts (Bug?)

I have got a strange problem: I am writing an Ocaml library
which uses some C code.  However, when I try to link this library to
an application, I get consistently some strange error (see below).  I
guess I am building the library incorrectly, but I have tried a
gazillion of variations to no avail.

Here a simple example:

First, the library.

Interface mylib.mli:

val hello: unit -> string


external hello: unit -> string = "hello"

Real implementation mylibcall.c:

#include <string.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>

value hello(void)
	static const char text[] = "hello";
	static const int len = 5;

	ret = alloc_string(len);
	strncpy(String_val(ret), text, len);

That's it.  Now I build it:

$ ocamlc.opt -c mylib.mli

$ ocamlopt.opt -c

$ cc -c -DNATIVE_CODE -I /usr/local/lib/ocaml mylibcall.c -o mylibcall.o

$ ocamlopt.opt -a mylibcall.o -o mylib.cmxa mylib.cmx

That's it, I leave all intermediate files where they are.

Now for the application (sources in another directory):


let _ = print_endline (Mylib.hello ())

Compiling it:

$ ocamlopt.opt -c -I /home/nils/src/caml/bug/mylib

And linking:

$ ocamlopt.opt -I /home/nils/src/caml/bug/mylib \
      -ccopt -L/home/nils/src/caml/bug/mylib  mylib.cmxa \
      -o myapp myapp.cmx
gcc: mylibcall.o: No such file or directory
Error during linking

If I copy mylibcall.o into the directory with the application sources,
everything workes fine.  But I wouldn't want to ask any users of the
library to copy that damned .o file into all their directories :-)

What am I doing wrong?  Because it seems to work for instance with the
lablGL libraries I generated on this machine :-(

Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x42B32FC9

Bug reports:  FAQ:
To unsubscribe, mail  Archives: