From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p8EF1b61023797 for ; Wed, 14 Sep 2011 17:01:37 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjkCAG3AcE68pTAdmWdsb2JhbABBmS+OTAEBAQEBCAsLBxQmghJAPRYYAwIBAgE3ASAIAod1lgmfYoZuBJh5i20 X-IronPort-AV: E=Sophos;i="4.68,380,1312149600"; d="scan'208";a="119822127" Received: from 11.mo1.mail-out.ovh.net (HELO mo1.mail-out.ovh.net) ([188.165.48.29]) by mail1-smtp-roc.national.inria.fr with ESMTP; 14 Sep 2011 17:01:30 +0200 Received: from mail174.ha.ovh.net (b6.ovh.net [213.186.33.56]) by mo1.mail-out.ovh.net (Postfix) with SMTP id DCD95FFBE12 for ; Wed, 14 Sep 2011 17:02:12 +0200 (CEST) Received: from b0.ovh.net (HELO queueout) (213.186.33.50) by b0.ovh.net with SMTP; 14 Sep 2011 17:01:29 +0200 Received: from unknown (HELO ?138.231.81.43?) (romain%bardou.fr@138.231.81.43) by ns0.ovh.net with SMTP; 14 Sep 2011 17:01:22 +0200 Message-ID: <4E70C18F.3040304@lri.fr> Date: Wed, 14 Sep 2011 17:00:31 +0200 From: Romain Bardou User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.20) Gecko/20110820 Icedove/3.1.12 MIME-Version: 1.0 To: caml-list@inria.fr X-Ovh-Mailout: 178.32.228.1 (mo1.mail-out.ovh.net) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Ovh-Tracer-Id: 13961721796838484512 X-Ovh-Remote: 138.231.81.43 () X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|U 0.5/N X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -20 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrfeeftddrfeduucetggdotefuucfrrhhofhhilhgvmecuqfggjfenuceurghilhhouhhtmecufedttdenuchlihhnuhigvdigucdlqddvtddm X-Validation-by: bardou@lri.fr Subject: [Caml-list] Link a .so/.dll dynamically 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