From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 B5A497F64D for ; Fri, 8 Jan 2016 16:24:54 +0100 (CET) IronPort-PHdr: 9a23:2MVxpxEbWJXSaOr/FwH40J1GYnF86YWxBRYc798ds5kLTJ75r8WwAkXT6L1XgUPTWs2DsrQf27SQ6/iocFdDyKjCmUhKSIZLWR4BhJdetC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TWM5DIfUi/yKRBybrysXNWC0YLvj6ibwN76XUZhvHKFe7R8LRG7/036l/I9ps9cEJs30QbDuXBSeu5blitCLFOXmAvgtI/rpMYwuwwZgf8q9tZBXKPmZOx4COUAVHV1BVso/9XmvgXvSg6G531UEjlH00kAPw+QyRj+VZD1t2Pfv/Z03CqTdZnzSLkoUDCh5o9kTRbpjGEMMDtvo0/NjckloKtFoR7pnBxyzpLPKNWONPdzYrj1dNcHQCxEX9oXWiBcVNDvJ7ATBvYMaL4L57L2oEED+F7jXAQ= Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=kosmo.zb@gmail.com; spf=Pass smtp.mailfrom=kosmo.zb@gmail.com; spf=None smtp.helo=postmaster@mail-yk0-f175.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of kosmo.zb@gmail.com) identity=pra; client-ip=209.85.160.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="kosmo.zb@gmail.com"; x-sender="kosmo.zb@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of kosmo.zb@gmail.com designates 209.85.160.175 as permitted sender) identity=mailfrom; client-ip=209.85.160.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="kosmo.zb@gmail.com"; x-sender="kosmo.zb@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-yk0-f175.google.com) identity=helo; client-ip=209.85.160.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="kosmo.zb@gmail.com"; x-sender="postmaster@mail-yk0-f175.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0ADCgAl1I9Wm6+gVdFehAxtBohTo0cGkhCHBgc8EAEBAQEBAQEBEAEBAQEBBgsLCSEugi2CCAEBBBIRBBkBLQYFAQMMAQUFCwMKAgIJHQICIQESAQUBChIGEwgKEId4AxIOozeBMT4xi0iEY4g0JwMKSIIuDBsBBQ5zg0SHEIJPhSSBSQWGHQyQZIVDhh2BeIFcSoxVhnGDaoInESSBFzllgVccgV0+NIVhAQEB X-IPAS-Result: A0ADCgAl1I9Wm6+gVdFehAxtBohTo0cGkhCHBgc8EAEBAQEBAQEBEAEBAQEBBgsLCSEugi2CCAEBBBIRBBkBLQYFAQMMAQUFCwMKAgIJHQICIQESAQUBChIGEwgKEId4AxIOozeBMT4xi0iEY4g0JwMKSIIuDBsBBQ5zg0SHEIJPhSSBSQWGHQyQZIVDhh2BeIFcSoxVhnGDaoInESSBFzllgVccgV0+NIVhAQEB X-IronPort-AV: E=Sophos;i="5.20,539,1444687200"; d="scan'208";a="196309048" Received: from mail-yk0-f175.google.com ([209.85.160.175]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 08 Jan 2016 16:24:53 +0100 Received: by mail-yk0-f175.google.com with SMTP id v14so271968170ykd.3 for ; Fri, 08 Jan 2016 07:24:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=8Kg+jd057NUcDUFooTjn4ZctW3n8DsdddVLdZcXdBGA=; b=sGx9YI41Puk+D/1kbuk5TIXTlNVZw0jn/E1AeADVpKu9MM01lq6FxShr+MT3L6GRhr xBGNcZpAzpo5iAUbT5fCwDtF6BpNiph85LkQn5sdvrJg6D0TN6fhrRvwZ+vLS6OPLPgC KIB0iGdIjnxxc4gT46PjdwHLuUBNm9CCfj8A6iGroj8wegqcBA08hKHdPdaWQGYuklm2 l0r2ELyPJx7K++MJYK4eaPzYj6t/079lD/FE2fIt7iFPWf4e4+fDoMhdp6IQ7bU0bPmL FVLJ+nNRw/l+Vduvcw6lElJemCNc/VBA3tPZEiDlzK5ytR4x/3bIyChdjICZCHIqSLKc im4Q== MIME-Version: 1.0 X-Received: by 10.129.123.134 with SMTP id w128mr58822862ywc.345.1452266692559; Fri, 08 Jan 2016 07:24:52 -0800 (PST) Sender: kosmo.zb@gmail.com Received: by 10.129.38.10 with HTTP; Fri, 8 Jan 2016 07:24:52 -0800 (PST) In-Reply-To: <86egdsnlof.fsf@gmail.com> References: <86r3hsnoox.fsf@gmail.com> <86egdsnlof.fsf@gmail.com> Date: Fri, 8 Jan 2016 15:24:52 +0000 X-Google-Sender-Auth: 1wwBQYakwQtMkLVTgKHScQEahTg Message-ID: From: David Sheets To: Malcolm Matalka Cc: O Caml Content-Type: text/plain; charset=UTF-8 Subject: Re: [Caml-list] Including a C library statically in an Ocaml library On Fri, Jan 8, 2016 at 3:19 PM, Malcolm Matalka wrote: > David Sheets writes: > >> On Fri, Jan 8, 2016 at 2:14 PM, Malcolm Matalka wrote: >>> The core problem I am having is a C library I want to bind has a number >>> of macros which I need the value of. Here is how I am trying to solve >>> it, but perhaps there is a better way: >>> >>> I have a small C library which gets compiled to libfoo.a which provides >>> functions that return the macro values, like: >>> >>> int macro1() { return MACRO1; } >>> >>> I then have an ocaml library, called ofoo, that uses Ctypes to bind to macro1: >>> >>> let macro1 = foreign "macro" (void @-> returning int) >>> >>> Where I am having issues is this small library, I'd prefer it to not >>> have to be installed on the system but just compiled into the Ocaml >>> library so that a user just has to link against that library. Right >>> now, none of the symbols (macro1) are being included in the library, I'm >>> guessing because the linker sees no direct use of them. And I'm not >>> even sure if I can get it included in the ocaml library. I'm also not >>> able to get the libfoo symbols linked into a final executable, I'm >>> guessing for similar reasons. >>> >>> What are my options here? >> >> I'd recommend using ctypes 0.4+ stub generation support which can bind >> macro values and detect struct layout at an early compilation stage. >> You can see it in use to do this in my ocaml-unix-errno library >> . One benefit with this >> approach is greater static checking and c <-> ocaml type safety. >> >> If you continue using dynamic bindings, two lonker flags may be of use to you: >> >> --no-as-needed : on recent Ubuntu distributions, gcc automatically >> includes the --as-needed flag which drops symbols that are not >> referenced. Unfortunately, clang does not understand this flag so you >> need to have a conditional in your build system to detect the compiler >> in use if you want a cross-platform library. >> >> -E : The Exports local symbols which are statically linked into a >> binary into the dynamic symbol table so that they can be found with >> dlsym. >> >> You can use these with gcc like -Wl,-E for example. > > Will these approaches require that I have the C library installed to > compile against any binary using my library or will the symbols be part > of the ocaml library? In my current version I have a libfoo.a that gets > created in the project and then linked against the library. In the stub generation case, you will not have symbols that represent the macros. Instead, C code will be generated which generates OCaml code which contains the macro constants. In the dlsym static linking case, you would build regular cm(x)a files and a .a file and install them all with ocamlfind. Your users don't have to know about C libraries or have them installed globally on the system. David >> >> Hope this helps, >> >> David Sheets >> >> >>> If I've missed any useful information, let me know. I haven't interoped >>> much with C directory in Ocaml so I'm not sure what information is >>> important. >>> >>> /Malcolm >>> >>> -- >>> 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