From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 411E47F890 for ; Wed, 26 Mar 2014 18:26:09 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of milanst@gmail.com) identity=pra; client-ip=209.85.213.177; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="milanst@gmail.com"; x-sender="milanst@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of milanst@gmail.com designates 209.85.213.177 as permitted sender) identity=mailfrom; client-ip=209.85.213.177; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="milanst@gmail.com"; x-sender="milanst@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ig0-f177.google.com) identity=helo; client-ip=209.85.213.177; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="milanst@gmail.com"; x-sender="postmaster@mail-ig0-f177.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhACAPsMM1PRVdWxlGdsb2JhbABEFYNBV4MLqQKWaIEVCBYOAQEBAQcLCwkSKoIlAQEBBCMEGQEbEgYFAQMMBgULDQICCR0CAiEBAREBBQEKEgYTEodSAQMRDaF1izVZUYMOllUKGScDCmSGXhEBBQyBHYssHIFNMweCb4FJBJZggW2BM4s2g1sYKYR4IQ X-IPAS-Result: AhACAPsMM1PRVdWxlGdsb2JhbABEFYNBV4MLqQKWaIEVCBYOAQEBAQcLCwkSKoIlAQEBBCMEGQEbEgYFAQMMBgULDQICCR0CAiEBAREBBQEKEgYTEodSAQMRDaF1izVZUYMOllUKGScDCmSGXhEBBQyBHYssHIFNMweCb4FJBJZggW2BM4s2g1sYKYR4IQ X-IronPort-AV: E=Sophos;i="4.97,736,1389740400"; d="scan'208";a="64881320" Received: from mail-ig0-f177.google.com ([209.85.213.177]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 26 Mar 2014 18:26:08 +0100 Received: by mail-ig0-f177.google.com with SMTP id ur14so774153igb.4 for ; Wed, 26 Mar 2014 10:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=ufVyo77XngVvmTFn6NwA30531UNyzzoQZ2bQRmlyI6A=; b=prHYx4fUDj93SJxnDpmiGW5Y4ts9uiOqC9iL3AQWvb/P2eVob9EB9hmTyrZKP/OQZQ 4HgCS+PG4UIHE202fg480+pA84nep8sdHP0ItvkWOIpmwR9enwOX2r5gJVoCPpRkk/tU na5JgeHLDhY9jkHvCIS46A4eoAFM16zw2Dbg0pt5y+8D3tGWa+r/6yDVjpAneguNmudM 9EFGe/g3Ece7gHnm0p3Qb+/XyQZ/cP/YrbWKypiOhe3Fod5Muulnr6qBfdDk9+P364HI ZQDtt0QODI1ktJylpxoSpbbmRfHTZEni7d6cHmc2DG4vrLb03Gd1q1ugUv7ydGnj9aa8 LzNA== X-Received: by 10.50.28.101 with SMTP id a5mr25680973igh.46.1395854767162; Wed, 26 Mar 2014 10:26:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.64.28.231 with HTTP; Wed, 26 Mar 2014 10:25:26 -0700 (PDT) In-Reply-To: References: From: =?UTF-8?Q?Milan_Stanojevi=C4=87?= Date: Wed, 26 Mar 2014 13:25:26 -0400 Message-ID: To: Jeremy Yallop Cc: Dan Benjamin , David Sheets , Bruno Deferrari , O Caml Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Caml-list] Static linking via Ctypes? Interesting. Thanks for the explanation. Just wanted to add that what David Sheets suggested worked for us (Jane Street) without need for any special linker flags apart from what our build system already does for C files which I think is pretty standard (compiles all C files into a library that is then linked into the packed ocaml library). But it's just one specific linux distribution On Tue, Mar 25, 2014 at 7:14 PM, Jeremy Yallop wrote: > I wrote: >> Not exactly, since the stub generation support in the next ctypes >> release resolves these issues in a different way. In the current >> release (0.2.3) setting up foreign calls and linking are both entirely >> dynamic. Stub generation will make it possible to link statically as >> well. > > Milan Stanojevi=C4=87 asked: >> Can you tell us how would this work exactly? >> Let's say I have a function "foo" in my C file (that I want to link >> statically) and I want to give use it in ocaml with a ctype (void @-> >> returning int) >> What would I need to say in my ocaml program and do I have to tell >> anything to my build system? > > With the current release of ctypes we'd bind 'foo' like this: > > let f =3D foreign "foo" (void @-> returning int) > > The 'foreign' function searches for the symbol "foo" and sets up the > call; the return value is an OCaml function that we can call > immediately. > > val f : unit -> int > > With stub generation as implemented in the git master branch the call > to 'foreign' changes slightly. Instead of calling 'foreign' just > once, we're going to call it once for each stage of generation. The > way we do this is to abstract over 'foreign' with a functor. (Note > the similarity to the original binding of 'f' above.) > > module Bindings (F : Cstubs.FOREIGN) =3D > struct > let f =3D F.foreign "foo" (void @-> returning int) > end > > The first application of the functor generates C: > > Cstubs.write_c std_formatter ~prefix:"foo_" (module Bindings) > > The output is a wrapper for the C function 'foo' that marshals and > unmarshals arguments and return values as needed. > > The second application generates OCaml: > > Cstubs.write_ml std_formatter ~prefix:"foo_" (module Bindings) > > The output is an OCaml module that we can pass to 'Bindings' for the > third and final application, which links together the declaration of > 'f' and the generated code: > > Bindings(Generated_ML) > > As with the dynamic binding, this gives us an OCaml function that we > can call without further ado: > > val f : unit -> int > > The build process is reasonably straightforward. Since we're > generating code, there are more steps than with the dynamic version, > but we're less likely to need obscure linker flags. A typical > approach is to place the bindings functor in one file -- > foo_binding.ml, say: > > module Bindings(F : Cstubs.FOREIGN) =3D > struct > let foo =3D F.foreign "foo" Ctypes.(void @-> returning int) > end > > and the calls to 'write_c' and 'write_ml' other files. I'll put the > call to 'write_c' along with code to print out the headers used in > generate_c.ml: > > let () =3D > print_endline "#include "; > print_endline "#include \"foo.h\""; > Cstubs.write_c Format.std_formatter ~prefix:"foo_" > (module Foo_binding.Bindings) > > The call to 'write_ml' goes in generate_ml.ml: > > let () =3D Cstubs.write_ml Format.std_formatter ~prefix:"foo_" > (module Foo_binding.Bindings) > > Then we can compile and run the two programs to generate the stub code: > > $ ocamlfind ocamlc -c -package ctypes.stubs \ > foo_binding.ml generate_c.ml generate_ml.ml > $ ocamlfind ocamlc -o generate_ml.native -linkpkg -package ctypes.stubs= \ > foo_binding.cmo generate_ml.cmo > $ ./generate_ml.native > generated.ml > $ ocamlfind ocamlc -o generate_c.native -linkpkg -package ctypes.stubs \ > foo_binding.cmo generate_c.cmo > $ ./generate_c.native > generated_c_stubs.c > > We can then compile the stub code and link it in with the bindings > functor and the library containing 'foo': > > $ ocamlfind ocamlc -c -package ctypes.stubs -I `ocamlc -where`/.. \ > generated_c_stubs.c generated.ml > $ ocamlfind ocamlmklib -o foo -linkpkg -package ctypes.stubs \ > generated_c_stubs.o generated.cmo foo_binding.cmo -L. -lfoo > > Finally, we can load the code and call the 'foo' function: > > $ ocaml -short-paths > OCaml version 4.01.0 > > # #use "topfind";; > [...] > # #require "ctypes.stubs";; > [...] > # #load "foo.cma";; > # include Foo_binding.Bindings(Generated);; > val foo : unit -> int =3D > # foo ();; > foo called > - : int =3D 3 > > The full stub generation interface (which is actually just the > 'write_ml' and 'write_c' functions) is here: > > https://github.com/ocamllabs/ocaml-ctypes/blob/1e1634/src/cstubs/cstub= s.mli > > Jeremy > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs