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 DAF867F64A for ; Fri, 8 Jan 2016 16:09:01 +0100 (CET) IronPort-PHdr: 9a23:cgv8TBWeqVe93j/z7XkQ2dsi1LHV8LGtZVwlr6E/grcLSJyIuqrYZhyEt8tkgFKBZ4jH8fUM07OQ6PC+HzRYqb+681k8M7V0HycfjssXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aJBzzOEJPK/jvHcaK1oLsh770o8WbSj4LrQT+SIs6FA+xowTVu5teqqpZAYF19CH0pGBVcf9d32JiKAHbtR/94sCt4MwrqHwI6LoJvvRNWqTifqk+UacQTHF/azh0t4XXskzoTRGO/WcbSmUbiFJ6Ag3A4Qv/V5H3+n/xtvB8wzWdJc37V5g7XDm466ZtDhPl3nQpLTk8pUTejsd3iKcThRu9rhh8ysaAZYScKPN6far1ctYTRG4HVcFUAX8SSrigZpcCWrJSdd1TqJPw8h5X9UOz Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=mmatalka@gmail.com; spf=Pass smtp.mailfrom=mmatalka@gmail.com; spf=None smtp.helo=postmaster@mail-wm0-f43.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of mmatalka@gmail.com) identity=pra; client-ip=74.125.82.43; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="mmatalka@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of mmatalka@gmail.com designates 74.125.82.43 as permitted sender) identity=mailfrom; client-ip=74.125.82.43; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="mmatalka@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-wm0-f43.google.com) identity=helo; client-ip=74.125.82.43; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="postmaster@mail-wm0-f43.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0B1AACn0I9WlCtSfUpehHmIWbNJAQ2BZIYPAoEeOBQBAQEBAQEBARABAQEBBwsLCR8wgi2CBwEBAQMBEhUZARsdAQMBCwYFCw0JJQ8BBA0CEQEFASITIod3AQMKCASjRIExPjGNMoJ5iBEKGScNV4IfAQEBAQEBAQECAQEBAQEBFwEFDoQ3ghGEf4JPhm0FjTmJVItggXiBXIc8hWOGcYNqgic1gRcgAQGCRg0VB4FdcoVhAQEB X-IPAS-Result: A0B1AACn0I9WlCtSfUpehHmIWbNJAQ2BZIYPAoEeOBQBAQEBAQEBARABAQEBBwsLCR8wgi2CBwEBAQMBEhUZARsdAQMBCwYFCw0JJQ8BBA0CEQEFASITIod3AQMKCASjRIExPjGNMoJ5iBEKGScNV4IfAQEBAQEBAQECAQEBAQEBFwEFDoQ3ghGEf4JPhm0FjTmJVItggXiBXIc8hWOGcYNqgic1gRcgAQGCRg0VB4FdcoVhAQEB X-IronPort-AV: E=Sophos;i="5.20,539,1444687200"; d="scan'208";a="196305116" Received: from mail-wm0-f43.google.com ([74.125.82.43]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 08 Jan 2016 16:09:01 +0100 Received: by mail-wm0-f43.google.com with SMTP id f206so139495576wmf.0 for ; Fri, 08 Jan 2016 07:09:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=LTIoEXEUzJqg88zuidVK0RjqW0SP4yGNc01inuUZ7BM=; b=mZnoiY0Ud26eD2V9eddnQ0C9F38HKb6RyHWQryNS8ZOM+48avFAaOYYgalyptfMu/e FfLCP9Q3cyHwfG1MPQrIZlTXDs2hfTPN4l9fST22knenJdTeFbnKI/PNHuEu3M4zq/kD Iebj7QrSUKXbmdPqgKXtdRcH78+WsBpd+ca/PQq4j4gabQElsgfHnc0CIhZ3l8kJFOBF FVmGRXpLB/LC0kEB33qv28GucVB3JX6Ingz5VH7kJcQzW9/u2f8fuejYVgzrStffXM6N ycZySFJaSpTjtNiKOa+IXxZWP9rPYi7YMg+HI/Iy5o4HdV6sdSMLTnZW5pV+9Qo16fWw Dpzg== X-Received: by 10.194.206.69 with SMTP id lm5mr113736221wjc.84.1452265741007; Fri, 08 Jan 2016 07:09:01 -0800 (PST) Received: from localhost (li195-236.members.linode.com. [178.79.139.236]) by smtp.gmail.com with ESMTPSA id xx3sm47577234wjc.32.2016.01.08.07.08.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Jan 2016 07:08:59 -0800 (PST) From: Malcolm Matalka To: Christopher Zimmermann Cc: caml-list@inria.fr References: <86r3hsnoox.fsf@gmail.com> <20160108155813.118c909b@mortimer.gmerlin.de> Date: Fri, 08 Jan 2016 15:08:57 +0000 In-Reply-To: <20160108155813.118c909b@mortimer.gmerlin.de> (Christopher Zimmermann's message of "Fri, 8 Jan 2016 15:58:13 +0100") Message-ID: <86mvsgnm5y.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Caml-list] Including a C library statically in an Ocaml library Christopher Zimmermann writes: > On Fri, 08 Jan 2016 14:14:22 +0000 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, > > That's no surprise. Macros are evaluated at compile time. You won't see > their name (only their value) in the compiled object file. This is why I'm creating a function that returns the macro value. > >> 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