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 8A1667EEFA for ; Tue, 10 Nov 2015 16:41:23 +0100 (CET) IronPort-PHdr: 9a23:4ShNIBJX9az8+DLaNNmcpTZWNBhigK39O0sv0rFitYgUKfjxwZ3uMQTl6Ol3ixeRBMOAu68C17ed7PCocFdDyKjCmUhKSIZLWR4BhJdetC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TWM5DIfUi/yKRBybrysXNWC0oLriqvtq9X6WEZhunmUWftKNhK4rAHc5IE9oLBJDeIP8CbPuWZCYO9MxGlldhq5lhf44dqsrtY4q3wD86Fpy8kVWqz/e+E8TKdEJDUgKWE8osPx5jfZSg7aynICU2leqRFPHxSNuArxWYvt9Cf4nuV40Siee8bxSOZnCnyZ8653RUqw2288PDkj/TSPhw== Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=scott.dj@gmail.com; spf=Pass smtp.mailfrom=scott.dj@gmail.com; spf=None smtp.helo=postmaster@mail-wm0-f54.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of scott.dj@gmail.com) identity=pra; client-ip=74.125.82.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="scott.dj@gmail.com"; x-sender="scott.dj@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of scott.dj@gmail.com designates 74.125.82.54 as permitted sender) identity=mailfrom; client-ip=74.125.82.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="scott.dj@gmail.com"; x-sender="scott.dj@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-f54.google.com) identity=helo; client-ip=74.125.82.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="scott.dj@gmail.com"; x-sender="postmaster@mail-wm0-f54.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BHAQD1DkJWlDZSfUpeg1o0bwatIYF6hQaKLIFlFwENhTE6gTwHOhIBAQEBAQEBARABAQEBBwsLCR8wgi6CIBEdARseAxIDDQcwAiQBEQEFAQ5Jh3YBAxINlRSNRYIIgTE+MYtIgWqCeYZfChknDVaDfwEaAQUOkEyCMwwuE4ExBZJng2F8hCGFUYI4XIEVM5JUhgASJIEXKAaCOSMdgUA+NAWFKQEBAQ X-IPAS-Result: A0BHAQD1DkJWlDZSfUpeg1o0bwatIYF6hQaKLIFlFwENhTE6gTwHOhIBAQEBAQEBARABAQEBBwsLCR8wgi6CIBEdARseAxIDDQcwAiQBEQEFAQ5Jh3YBAxINlRSNRYIIgTE+MYtIgWqCeYZfChknDVaDfwEaAQUOkEyCMwwuE4ExBZJng2F8hCGFUYI4XIEVM5JUhgASJIEXKAaCOSMdgUA+NAWFKQEBAQ X-IronPort-AV: E=Sophos;i="5.20,271,1444687200"; d="scan'208";a="186960033" Received: from mail-wm0-f54.google.com ([74.125.82.54]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 10 Nov 2015 16:41:23 +0100 Received: by wmww144 with SMTP id w144so123151974wmw.1 for ; Tue, 10 Nov 2015 07:41:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=KwlCm0NEoV1F7aX4QSV4i7kj4O2Cy9d08kuhqqKS2jU=; b=uW4jbCDlxEHplAGvrZV7ZxwHpCM2ySqHDDdBWYh/+2XRb+50nLOn0O7NWQvmwmVIp2 z1r3/rGWVPwC1b1O9LgxmjuPU01jKw2C5bZdgl3vX6xFWMLMG0oQFxf/kVUw6a0+kjDT JD1epAr8swxjFWTfH4Z0z4IwVBouxhZCydZzYyO17uZlwK7o0zqzW1vtp+ws3DcunvHV nXHy2zLvO4bFujnMle0QobQWJptbZyn0QCuTl3hqsNLCF6+lqtRcjN7VJxUvO8Kvma+t GMwxEkLpgeuDOeuSOQ+AbhvNUYUyTdv0YHOmG7ourZyXeBn1+irXoLS4b8i/ZGM1eH/C py5Q== MIME-Version: 1.0 X-Received: by 10.28.143.79 with SMTP id r76mr5867044wmd.102.1447170082859; Tue, 10 Nov 2015 07:41:22 -0800 (PST) Received: by 10.28.88.135 with HTTP; Tue, 10 Nov 2015 07:41:22 -0800 (PST) Date: Tue, 10 Nov 2015 15:41:22 +0000 Message-ID: From: David Scott To: caml-list@inria.fr Content-Type: multipart/alternative; boundary=001a11468328b7f6680524318a01 Subject: [Caml-list] Simplifying link steps when making shared libraries containing OCaml (possibly OSX specific) --001a11468328b7f6680524318a01 Content-Type: text/plain; charset=UTF-8 Hi, I've packaged up some OCaml code with a C shim and made a shared library (a ".so" file) on OSX. It's all working fine! However the build steps I'm using are a bit verbose -- I wonder if anyone knows how to simplify them? I'm building all the objects and then using a custom linker invocation, see below. Notice that I'm tediously listing all the stubs for all my ocamlfind packages-- this seems like it could be simplified somehow. ``` PACKS="cstruct lwt io-page...." WHERE=$(ocamlc -where) cd _build ocamlfind ocamlopt -thread -package "$PACKS" -c ../c_callbacks.ml -o c_callbacks.cmx ocamlfind ocamlopt -thread -linkpkg -package "$PACKS" -output-obj -o camlcode.o c_callbacks.cmx gcc -I ${WHERE} -g -Wall -Wextra -c ../c_shim.c -o c_shim.o gcc -shared camlcode.o c_shim.o -ldl -lm -L ${WHERE} \ $(ocamlfind query cstruct)/cstruct.a \ $(ocamlfind query cstruct)/libcstruct_stubs.a \ $(ocamlfind query io-page)/io_page.a \ $(ocamlfind query io-page)/io_page_unix.a \ $(ocamlfind query io-page)/libio_page_unix_stubs.a \ $(ocamlfind query lwt.unix)/liblwt-unix_stubs.a \ $(ocamlfind query lwt.unix)/lwt-unix.a \ $(ocamlfind query lwt.unix)/lwt.a \ $(ocamlfind query threads)/libthreadsnat.a \ -lasmrun -lbigarray -lunix -o my_shared_library.so ``` I tried following Jeremy's inverted ctypes example here: https://github.com/yallop/ocaml-ctypes-inverted-stubs-example/blob/ff713bc9a9159828b2c3d7e6661f7ba76855e8b7/Makefile#L29 This looks good because it relies on ocamlfind + ocamlopt to do the right thing but unfortunately it fails on my OSX box because it produces a "bundle" (with "-bundle" linker option) rather than a shared library (with a "-shared" linker option): When ocamlopt builds the ctypes-inverted-stubs-example .so it's one of these: ``` $ file _build/libxmlm.so _build/libxmlm.so: Mach-O 64-bit bundle x86_64 ``` which then causes failures which look like ``` ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file '../_build/test/../libxmlm.so' for architecture x86_64 ``` When I do it by hand, I can replace "-bundle" with "-shared" which results in: ``` $ file _build/libxmlm.so _build/libxmlm.so: Mach-O 64-bit dynamically linked shared library x86_64 ``` which then links ok. Any tips greatly appreciated! Thanks, -- Dave Scott --001a11468328b7f6680524318a01 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi,

I've packaged up some OCaml cod= e with a C shim and made a shared library (a ".so" file) on OSX. = It's all working fine! However the build steps I'm using are a bit = verbose -- I wonder if anyone knows how to simplify them?

I'm building all the objects and then using a custom linker inv= ocation, see below. Notice that I'm tediously listing all the stubs for= all my ocamlfind packages-- this seems like it could be simplified somehow= .

```
PACKS=3D"cstruct lwt io-page.= ..."
WHERE=3D$(ocamlc -where)
cd _build
=
ocamlfind ocamlopt -thread -package "$PACKS" -c ../c_callbacks.ml -o c_callbacks.cmx
= ocamlfind ocamlopt -thread -linkpkg -package "$PACKS" -output-obj= -o camlcode.o c_callbacks.cmx
gcc -I ${WHERE} -g -Wall -Wextra = =C2=A0-c ../c_shim.c -o c_shim.o

gcc -shared c= amlcode.o c_shim.o -ldl -lm -L ${WHERE} \
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 $(ocamlfind query cstruct)/cstruct.a \
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 $(ocamlfind query cstruct)/libcstruct_stubs.a \
=C2=A0= =C2=A0 =C2=A0 =C2=A0 $(ocamlfind query io-page)/io_page.a \
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 $(ocamlfind query io-page)/io_page_unix.a \
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 $(ocamlfind query io-page)/libio_page_unix_= stubs.a \
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $(ocamlfind query lwt.unix)= /liblwt-unix_stubs.a \
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $(ocamlfind qu= ery lwt.unix)/lwt-unix.a \
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $(ocamlfin= d query lwt.unix)/lwt.a \
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $(ocamlfind= query threads)/libthreadsnat.a \
=C2=A0 =C2=A0 =C2=A0 =C2=A0 -la= smrun -lbigarray -lunix -o my_shared_library.so
```

I tried following Jeremy's inverted ctypes example here= :

<= br>
This looks good because it relies on ocamlfind + ocamlopt to = do the right thing but unfortunately it fails on my OSX box because it prod= uces a "bundle" (with "-bundle" linker option) rather t= han a shared library (with a "-shared" linker option):
=
When ocamlopt builds the ctypes-inverted-stubs-example .so i= t's one of these:

```
$ file _b= uild/libxmlm.so=C2=A0
_build/libxmlm.so: Mach-O 64-bit bundle x86= _64
```

which then causes failures= which look like

```
ld: can't link = with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file '../_build/test/../= libxmlm.so' for architecture x86_64
```

When I do it by hand, I can replace "-bundle" with "= -shared" which results in:

```
$ fi= le _build/libxmlm.so=C2=A0
_build/libxmlm.so: Mach-O 64-bit d= ynamically linked shared library x86_64
```

which then links ok.

Any tips greatly appr= eciated!

Thanks,
--
Dave Scott
--001a11468328b7f6680524318a01--