From: Christopher Zimmermann <christopher@gmerlin.de>
To: caml-list@inria.fr
Cc: Malcolm Matalka <mmatalka@gmail.com>
Subject: Re: [Caml-list] Including a C library statically in an Ocaml library
Date: Fri, 8 Jan 2016 15:58:13 +0100 [thread overview]
Message-ID: <20160108155813.118c909b@mortimer.gmerlin.de> (raw)
In-Reply-To: <86r3hsnoox.fsf@gmail.com>
On Fri, 08 Jan 2016 14:14:22 +0000 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,
That's no surprise. Macros are evaluated at compile time. You won't see
their name (only their value) in the compiled object file.
> 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?
>
> 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.
You could hardcode the value of the macros into your ocaml code like
this:
let macro1 = 4096;;
But I don't see anything wrong with your approach. If you have really
many macros and don't want to add that many functions you could create
a static record of all the macro values in your C stub code like this
(not tested):
value get_constants(value) {
static value constants[5] = {
(4 << 10), /* header for block of size 4 */
Val_long(Macro1),
Val_long(Macro2),
NULL,
Val_long(Macro4)
};
if (constants.3 == NULL) {
constants.3 = caml_copy_string(Macro3);
}
return (constants + 1);
}
Christopher
--
http://gmerlin.de
OpenPGP: http://gmerlin.de/christopher.pub
F190 D013 8F01 AA53 E080 3F3C F17F B0A1 D44E 4FEE
next prev parent reply other threads:[~2016-01-08 14:58 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 [this message]
2016-01-08 15:08 ` Malcolm Matalka
2016-01-08 15:15 ` David Sheets
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=20160108155813.118c909b@mortimer.gmerlin.de \
--to=christopher@gmerlin.de \
--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