Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] How to link libraries with C parts (Bug?)
From: Nils Goesche <cartan@cartan.de>

> 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 mylib.ml
> 
> $ 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 myapp.ml
> 
> 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.

Cheers,

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr