From mboxrd@z Thu Jan 1 00:00:00 1970 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 q3Q8XgSm022092 for ; Thu, 26 Apr 2012 10:33:46 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlEDAGAHmU+wCYo3gWdsb2JhbABFgx2uLyIBARYmJ4IJAQEFJxkBATYCDwsYCRYPCQMCAQIBRRMIAogNpwiEMQGPSQaNQIMliGGNIIV0jTc X-IronPort-AV: E=Sophos;i="4.75,485,1330902000"; d="scan'208";a="155690440" Received: from mail.etorok.net ([176.9.138.55]) by mail1-smtp-roc.national.inria.fr with ESMTP; 26 Apr 2012 10:33:46 +0200 Received: from [192.168.1.101] (unknown [79.114.80.162]) by mail.etorok.net (Postfix) with ESMTPSA id A462E46A6 for ; Thu, 26 Apr 2012 10:33:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=etorok.net; s=MAILOUT; t=1335429225; bh=j2AqRXr1ZHjPlHvqHSPzl5nBIdPOoj8YYj/z6T/YX7g=; h=Message-ID:Date:From:MIME-Version:To:Subject:References: In-Reply-To:Content-Type:Content-Transfer-Encoding; b=Jwfa2uKvJaUYpqXgcOvvzKRbDb0pPw119b/FpFjxGELcY3Y3cCkOq+4Jsf+gGsd3+ xjwqZ/R6piOtqvaurojAcTfTKmE/9lSPV3W5h3XHi4nKzTauREsCg7VyyK5KhZ9rJ+ +SdGdhiuz+fQndcM3hcrG0g2Dht4tD6foIsKQGdg= Message-ID: <4F990869.5050105@etorok.net> Date: Thu, 26 Apr 2012 11:33:45 +0300 From: =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120329 Icedove/10.0.3 MIME-Version: 1.0 To: caml-list@inria.fr References: <4F99003B.9070001@inria.fr> In-Reply-To: <4F99003B.9070001@inria.fr> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: clamav-milter 0.97.3 at mail X-Virus-Status: Clean Subject: Re: [Caml-list] static C library of wrappers: compilation issues On 04/26/2012 10:58 AM, Xavier ALLAMIGEON wrote: > Dear caml-list, > > I'd like to build a static C library implementing an interface to an OCaml library, but I get some compilation errors. Here's an example of the problem. > > 1) The ml_code.* files contain a hello_world function which I'd like to provide in C. > > > let ml_hello_world () = > print_endline "Hello world!" > > let _ = > Callback.register "ml_hello_world" ml_hello_world > > > val ml_hello_world: unit -> unit > > 2) On the C part, I created .c/.h files calling the ml function with camlback: > > #include > #include > > void init(void) { > char* dummy = '\0'; > caml_main(&dummy); > } > > void c_hello_world(void) { > CAMLparam0(); > static value *closure_ml_hello_world = NULL; > if (closure_ml_hello_world == NULL) { > closure_ml_hello_world = caml_named_value("ml_hello_world"); > } > caml_callback(*closure_ml_hello_world, Val_unit); > CAMLreturn0; > } > > > void init(void); > void c_hello_world(void); > > 3) Finally, I created a test file in C: > > #include "c_code.h" > > int main(int argc, char **argv) { > init(); > c_hello_world(); > } > > Here's the way I'm compiling everything. It builds a libhello_world.a static library from ml_code.obj.o and c_code.o. > ocamlc -c ml_code.mli > ocamlopt -c ml_code.ml > ocamlopt -output-obj ml_code.cmx -o ml_code.obj.o > gcc -c c_code.c -I"`ocamlc -where`" > ar rcs libhello_world.a ml_code.obj.o c_code.o > gcc -o test -L. -L"`ocamlc -where`" test.c -lhello_world -lasmrun -lm -ldl You need -Wl,--whole-archive -Wl,--no-whole-archive around the link of your .a, like this: gcc -o test -L. -L"`ocamlc -where`" test.c -Wl,--whole-archive -lhello_world -Wl,--no-whole-archive -lasmrun -lm -ldl Otherwise the functions from your .a are dropped because nothing else before it needs it (-lasmrun would, but its after your .a). Best regards, --Edwin