caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Simplifying link steps when making shared libraries containing OCaml (possibly OSX specific)
@ 2015-11-10 15:41 David Scott
  2015-11-10 18:20 ` John Whitington
  0 siblings, 1 reply; 2+ messages in thread
From: David Scott @ 2015-11-10 15:41 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2363 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 3450 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Simplifying link steps when making shared libraries containing OCaml (possibly OSX specific)
  2015-11-10 15:41 [Caml-list] Simplifying link steps when making shared libraries containing OCaml (possibly OSX specific) David Scott
@ 2015-11-10 18:20 ` John Whitington
  0 siblings, 0 replies; 2+ messages in thread
From: John Whitington @ 2015-11-10 18:20 UTC (permalink / raw)
  To: David Scott; +Cc: caml-list

Hi David,

David Scott wrote:
> 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.

No answers, I'm afraid, but if you do find a good generic way of doing 
it for OS X (and indeed, Linux), it would be great have patches to this:

https://github.com/johnwhitington/ocaml-main-program-in-c

(which only does static binaries when run under Unix or OS X, but both 
static binaries and dynamic libraries when under Windows.)

You can see that I didn't manage to find a way to automatically extract 
which ocamlfind packs are needed, or indeed if unix or bigarray is 
needed. They must be specified:

https://github.com/johnwhitington/ocaml-main-program-in-c/blob/master/build

Thanks,

John

-- 
John Whitington
Director, Coherent Graphics Ltd
http://www.coherentpdf.com/


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-11-10 18:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-10 15:41 [Caml-list] Simplifying link steps when making shared libraries containing OCaml (possibly OSX specific) David Scott
2015-11-10 18:20 ` John Whitington

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).