caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How to link libraries with C parts (Bug?)
@ 2001-06-08 19:45 Nils Goesche
  2001-06-12  2:19 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Nils Goesche @ 2001-06-08 19:45 UTC (permalink / raw)
  To: caml-list

Hi!

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

Implementation mylib.ml:

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;
	CAMLparam0();
	CAMLlocal1(ret);

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

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

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

Now for the application (sources in another directory):

Source myapp.ml:

let _ = print_endline (Mylib.hello ())

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
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 :-(

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

PGP key ID 0x42B32FC9

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] How to link libraries with C parts (Bug?)
  2001-06-08 19:45 [Caml-list] How to link libraries with C parts (Bug?) Nils Goesche
@ 2001-06-12  2:19 ` Jacques Garrigue
  2001-06-12 13:54   ` Nils Goesche
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2001-06-12  2:19 UTC (permalink / raw)
  To: cartan; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] How to link libraries with C parts (Bug?)
  2001-06-12  2:19 ` Jacques Garrigue
@ 2001-06-12 13:54   ` Nils Goesche
  0 siblings, 0 replies; 3+ messages in thread
From: Nils Goesche @ 2001-06-12 13:54 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> writes:

> From: Nils Goesche <cartan@cartan.de>
> 
> > $ 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

I tried

$ ar cr libmylibcall.a mylibcall.o

and everything works fine now.  I only tried _slight_ variations of
this :-)

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

PGP key ID 0x42B32FC9

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-06-12 13:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-08 19:45 [Caml-list] How to link libraries with C parts (Bug?) Nils Goesche
2001-06-12  2:19 ` Jacques Garrigue
2001-06-12 13:54   ` Nils Goesche

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).