Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: David Sheets <sheets@alum.mit.edu>
To: Malcolm Matalka <mmatalka@gmail.com>
Cc: O Caml <caml-list@inria.fr>
Subject: Re: [Caml-list] Including a C library statically in an Ocaml library
Date: Fri, 8 Jan 2016 15:15:41 +0000	[thread overview]
Message-ID: <CAAWM5TwCDSEDwx1CEqVG66WESricU4BtmeRan+3fvOT6S3tUTA@mail.gmail.com> (raw)
In-Reply-To: <86r3hsnoox.fsf@gmail.com>

On Fri, Jan 8, 2016 at 2:14 PM, Malcolm Matalka <mmatalka@gmail.com> 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
<https://github.com/dsheets/ocaml-unix-errno>. 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.

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

  parent reply	other threads:[~2016-01-08 15:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-08 14:14 Malcolm Matalka
2016-01-08 14:58 ` Christopher Zimmermann
2016-01-08 15:08   ` Malcolm Matalka
2016-01-08 15:15 ` David Sheets [this message]
2016-01-08 15:19   ` Malcolm Matalka
2016-01-08 15:24     ` David Sheets

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAAWM5TwCDSEDwx1CEqVG66WESricU4BtmeRan+3fvOT6S3tUTA@mail.gmail.com \
    --to=sheets@alum.mit.edu \
    --cc=caml-list@inria.fr \
    --cc=mmatalka@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox