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-12 (02:19)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] How to link libraries with C parts (Bug?)
From: Nils Goesche <>

> 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.


> 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

In order to use the autolink feature of ocaml, you should put your C
object inside a library. That is:

$ ar cf libmylibcall.a mylibcall.o
$ ocamlopt.opt -a -cclib -lmylibcall -o mylib.cmxa mylib.cmx

Then the rest should work correctly
> 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

You don't even need the -ccopt -L... : all directories marked by -I
are automatically added in the link.


Jacques Garrigue
Bug reports:  FAQ:
To unsubscribe, mail  Archives: