Article View: fa.caml
Article #138Re: [Caml-list] static C library of wrappers: compilation issues
From: Török Edwin
Date: Thu, 26 Apr 2012 08:34
Date: Thu, 26 Apr 2012 08:34
72 lines
5035 bytes
5035 bytes
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.
>
> <ml_code.ml>
> let ml_hello_world () =
> print_endline "Hello world!"
>
> let _ =
> Callback.register "ml_hello_world" ml_hello_world
>
> <ml_code.mli>
> val ml_hello_world: unit -> unit
>
> 2) On the C part, I created .c/.h files calling the ml function with camlback:
> <c_code.c>
> #include <caml/callback.h>
> #include <caml/memory.h>
>
> 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;
> }
>
> <c_code.h>
> void init(void);
> void c_hello_world(void);
>
> 3) Finally, I created a test file in C:
> <test.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
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Technical Details
Message-ID:
<fa.x1meoX1izKZMvjRMs57ceIS84Ic@ifi.uio.no>Path:
arc1-ca.pugleaf.net!r9ni99975pbh.0!nntp.google.com!news1.google.com!goblin3!goblin1!goblin.stu.neva.ru!uio.no!nntp.uio.no!.POSTED!not-for-mailReferences:
<fa.XlnNJXrM7cjr64FYE99G2A+nhk4@ifi.uio.no>Show Full Headers
Complete Article Headers
X-Google-Thread: ff861,9e13cd36b5c96a87
X-Google-NewGroupId: yes
X-Google-Attributes: gidc0fe4b917e,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.68.197.193 with SMTP id iw1mr5437150pbc.0.1335429246910;
Thu, 26 Apr 2012 01:34:06 -0700 (PDT)
Path: arc1-ca.pugleaf.net!r9ni99975pbh.0!nntp.google.com!news1.google.com!goblin3!goblin1!goblin.stu.neva.ru!uio.no!nntp.uio.no!.POSTED!not-for-mail
From: Török Edwin <edwin+ml-ocaml@etorok.net>
Newsgroups: fa.caml
Subject: Re: [Caml-list] static C library of wrappers: compilation issues
Date: Thu, 26 Apr 2012 08:34:06 UTC
Organization: Internet mailing list
Lines: 73
Message-ID: <fa.x1meoX1izKZMvjRMs57ceIS84Ic@ifi.uio.no>
References: <fa.XlnNJXrM7cjr64FYE99G2A+nhk4@ifi.uio.no>
NNTP-Posting-Host: mail-jess.uio.no
Mime-Version: 1.0
X-Trace: readme.uio.no 1335429246 4851 129.240.7.9 (26 Apr 2012 08:34:06 GMT)
X-Complaints-To: abuse@uio.no
NNTP-Posting-Date: Thu, 26 Apr 2012 08:34:06 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120329 Icedove/10.0.3
To: caml-list@inria.fr
X-IronPort-AV: E=Sophos;i="4.75,485,1330902000";
d="scan'208";a="155690525"
X-Authentication-Warning: walapai.inria.fr: sympa set sender to caml-list-owner@inria.fr using -f
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"
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=
In-Reply-To: <4F99003B.9070001@inria.fr>
X-Virus-Scanned: clamav-milter 0.97.3 at mail
X-Virus-Status: Clean
X-Sequence: 3968
List-Id: <caml-list.inria.fr>
List-Archive: <https://sympa-roc.inria.fr/wws/arc/caml-list>
List-Help: <mailto:sympa@inria.fr?subject=help>
List-Owner: <mailto:caml-list-request@inria.fr>
List-Post: <mailto:caml-list@inria.fr>
List-Subscribe: <mailto:sympa@inria.fr?subject=subscribe%20caml-list>
List-Unsubscribe: <mailto:sympa@inria.fr?subject=unsubscribe%20caml-list>
Original-Date: Thu, 26 Apr 2012 11:33:45 +0300
Original-Message-Id: <4F990869.5050105@etorok.net>
Original-References: <4F99003B.9070001@inria.fr>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit