caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Link a .so/.dll dynamically
@ 2011-09-14 15:00 Romain Bardou
  2011-09-14 15:35 ` Benedikt Meurer
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Romain Bardou @ 2011-09-14 15:00 UTC (permalink / raw)
  To: caml-list

Hello Caml-List,

I need to write a program which handles several devices. Each device has 
a .so (linux) or .dll (windows) driver. Each of these DLL share the same 
API, defined in C headers (api.h).

Here is what I figured I should do.
- Write a binding for the library.
   * OCaml part: mylib.ml, with externals.
   * C part: wrapper.c, implementing the externals.
     - Which itself includes api.h and may use the functions of the API.
- Dynamically load this library using Dynlink.
Later, the Mylib module will register each function of the API so the 
program can call them, but that's not the issue (although if there is a 
simpler way please tell).

So far, here is what I have achieved: I managed to dynamically load a 
Mylib module with externals in wrapper.c, but this wrapper.c file does 
not include nor use the API (it just prints some text on standard 
output). Here is how I compile the program:
	ocamlc -c wrapper.c
	ocamlc -c mylib.ml
	ocamlmklib wrapper.o mylib.cmo -o mylib
	ocamlc -c main.ml
	ocamlc dynlink.cma main.cmo -o main
	CAML_LD_LIBRARY_PATH=. ./main
The main.ml file uses Dynlink to load "mylib.cma". The 
CAML_LD_LIBRARY_PATH environment variable is needed for some reason, or 
the dynamic linker cannot find dllmylib.so.

My first problem is: I tried something similar for native code (using 
ocamlopt instead of ocamlc, .cmx instead of .cmo and .cmxa instead of 
.cma) but ocamlmklib fails with the following error:
	/usr/bin/ld: wrapper.o: relocation R_X86_64_32 against `.rodata' can 
not be used when making a shared object; recompile with -fPIC
	wrapper.o: could not read symbols: Bad value
	collect2: ld returned 1 exit status
I have absolutely no idea what this means, I'm really confused here and 
Google does not help.

My second, more important problem is: how do I link with the .so / .dll 
of my device? Obviously, once I use functions declared in api.h, the .so 
needs to be loaded or I get the "undefined symbol" error. Assume I want 
to link with /usr/lib/toto.so. How should I modify my compilation 
process to ensure that this driver will be dynamically loaded when Mylib 
is dynamically linked with Dynlink? I tried various combinations of 
parameters for ocamlmklib and none worked.

A third question will be: I'm testing this under Linux with .so files, 
will there be additional problems on Windows with .dll files?

Thank you for your help. As you can see, I'm a little confused about 
those things.

-- 
Romain Bardou

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

end of thread, other threads:[~2011-09-15  9:24 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-14 15:00 [Caml-list] Link a .so/.dll dynamically Romain Bardou
2011-09-14 15:35 ` Benedikt Meurer
     [not found] ` <83695D27-A767-438A-B909-6864D1A655FE@googlemail.com>
2011-09-14 15:56   ` Romain Bardou
2011-09-14 15:59     ` Benedikt Meurer
2011-09-14 16:03       ` Romain Bardou
2011-09-14 16:07         ` Jérémie Dimino
2011-09-14 16:07     ` Stéphane Glondu
2011-09-14 16:12       ` Romain Bardou
2011-09-14 16:34         ` Stéphane Glondu
2011-09-14 16:42           ` Romain Bardou
2011-09-14 16:56             ` Stéphane Glondu
2011-09-15  8:49               ` Romain Bardou
2011-09-15  9:20                 ` Stéphane Glondu
2011-09-15  9:23                   ` Romain Bardou
2011-09-15  9:05               ` Romain Bardou
2011-09-14 16:15 ` Jérémie Dimino

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