caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Building a mixed C / OCaml static library to be used from C
@ 2013-08-22 17:34 John Whitington
  2013-08-22 22:24 ` Gerd Stolpmann
  0 siblings, 1 reply; 3+ messages in thread
From: John Whitington @ 2013-08-22 17:34 UTC (permalink / raw)
  To: caml users

Hi,

I have the following recipe to build a static library of C functions 
from a mixed ocaml / c codebase. The idea is that the final linking step 
to build target 'test' won't require any special flags:


CAMLBASE = /Users/john/.opam/4.00.1/lib/

mklib: cpdflib.mli cpdflib.ml cpdflibwrapper.c
	ocamlfind ocamlc -package cpdf cpdflib.mli;
	ocamlfind ocamlopt -package cpdf -c cpdflib.ml;
	ocamlfind ocamlc cpdflibwrapper.c;
	ocamlfind ocamlopt -I $(CAMLBASE)cpdf -I $(CAMLBASE)camlpdf \
           -output-obj -o cpdflib.o \
           unix.cmxa bigarray.cmxa camlpdf.cmxa cpdf.cmxa cpdflib.cmx;
	ar -x $(CAMLBASE)camlpdf/libcamlpdf_stubs.a;
	ar -x $(CAMLBASE)ocaml/libasmrun.a;
	ar cr cpdflib.a *.o

test:   cpdflib.a cpdflibtest.c
	cc cpdflibtest.c cpdflib.a -o test\
           -L $(CAMLBASE)ocaml -lbigarray -lunix

clean:
	rm __.SYMDEF\ SORTED *.o *.cmx *.cmi *.a test


(Here, cpdflib.ml, cpdflib.mli and cpdflibwrapper.c form the C interface 
to the OCaml functions. cpdflibtest.c calls caml_startup and then any 
caml functions it likes).

This works. However, I'm having trouble getting rid of "-L 
$(CAMLBASE)ocaml -lbigarray -lunix" -- i'd like the person linking with 
my cpdflib.a not to need any dependencies at all.

When I add two more lines

         ar -x $(CAMLBASE)ocaml/bigarray.a;
         ar -x $(CAMLBASE)ocaml/unix.a;

in the obvious place and remove "-L $(CAMLBASE)ocaml -lbigarray -lunix" 
when building the example, the linking fails, not being able to find the 
symbols for unix and bigarray. ar -t confirms that bigarray.o, unix.o 
and unixlabels.o are in the created cpdflib.a

Any ideas, or another recipe altogether?

Thanks,

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


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

* Re: [Caml-list] Building a mixed C / OCaml static library to be used from C
  2013-08-22 17:34 [Caml-list] Building a mixed C / OCaml static library to be used from C John Whitington
@ 2013-08-22 22:24 ` Gerd Stolpmann
  2013-08-27 13:12   ` John Whitington
  0 siblings, 1 reply; 3+ messages in thread
From: Gerd Stolpmann @ 2013-08-22 22:24 UTC (permalink / raw)
  To: John Whitington; +Cc: caml users

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

Am Donnerstag, den 22.08.2013, 18:34 +0100 schrieb John Whitington:
> Hi,
> 
> I have the following recipe to build a static library of C functions 
> from a mixed ocaml / c codebase. The idea is that the final linking step 
> to build target 'test' won't require any special flags:
> 
> 
> CAMLBASE = /Users/john/.opam/4.00.1/lib/
> 
> mklib: cpdflib.mli cpdflib.ml cpdflibwrapper.c
> 	ocamlfind ocamlc -package cpdf cpdflib.mli;
> 	ocamlfind ocamlopt -package cpdf -c cpdflib.ml;
> 	ocamlfind ocamlc cpdflibwrapper.c;
> 	ocamlfind ocamlopt -I $(CAMLBASE)cpdf -I $(CAMLBASE)camlpdf \
>            -output-obj -o cpdflib.o \
>            unix.cmxa bigarray.cmxa camlpdf.cmxa cpdf.cmxa cpdflib.cmx;
> 	ar -x $(CAMLBASE)camlpdf/libcamlpdf_stubs.a;
> 	ar -x $(CAMLBASE)ocaml/libasmrun.a;
> 	ar cr cpdflib.a *.o
> 
> test:   cpdflib.a cpdflibtest.c
> 	cc cpdflibtest.c cpdflib.a -o test\
>            -L $(CAMLBASE)ocaml -lbigarray -lunix
> 
> clean:
> 	rm __.SYMDEF\ SORTED *.o *.cmx *.cmi *.a test
> 
> 
> (Here, cpdflib.ml, cpdflib.mli and cpdflibwrapper.c form the C interface 
> to the OCaml functions. cpdflibtest.c calls caml_startup and then any 
> caml functions it likes).
> 
> This works. However, I'm having trouble getting rid of "-L 
> $(CAMLBASE)ocaml -lbigarray -lunix" -- i'd like the person linking with 
> my cpdflib.a not to need any dependencies at all.
> 
> When I add two more lines
> 
>          ar -x $(CAMLBASE)ocaml/bigarray.a;
>          ar -x $(CAMLBASE)ocaml/unix.a;

These are the wrong two libs. bigarray.a and unix.a are already
contained in cpdflib.a. What you need are libbigarray.a and libunix.a.

Gerd

> 
> in the obvious place and remove "-L $(CAMLBASE)ocaml -lbigarray -lunix" 
> when building the example, the linking fails, not being able to find the 
> symbols for unix and bigarray. ar -t confirms that bigarray.o, unix.o 
> and unixlabels.o are in the created cpdflib.a
> 
> Any ideas, or another recipe altogether?
> 
> Thanks,
> 
> -- 
> John Whitington
> Director, Coherent Graphics Ltd
> http://www.coherentpdf.com/
> 
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

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

* Re: [Caml-list] Building a mixed C / OCaml static library to be used from C
  2013-08-22 22:24 ` Gerd Stolpmann
@ 2013-08-27 13:12   ` John Whitington
  0 siblings, 0 replies; 3+ messages in thread
From: John Whitington @ 2013-08-27 13:12 UTC (permalink / raw)
  To: Gerd Stolpmann; +Cc: caml users

Hi,

Gerd Stolpmann wrote:
> Am Donnerstag, den 22.08.2013, 18:34 +0100 schrieb John Whitington:
>> Hi,
>>
>> I have the following recipe to build a static library of C functions
>> from a mixed ocaml / c codebase. The idea is that the final linking step
>> to build target 'test' won't require any special flags:
>>
>>
>> CAMLBASE = /Users/john/.opam/4.00.1/lib/
>>
>> mklib: cpdflib.mli cpdflib.ml cpdflibwrapper.c
>> 	ocamlfind ocamlc -package cpdf cpdflib.mli;
>> 	ocamlfind ocamlopt -package cpdf -c cpdflib.ml;
>> 	ocamlfind ocamlc cpdflibwrapper.c;
>> 	ocamlfind ocamlopt -I $(CAMLBASE)cpdf -I $(CAMLBASE)camlpdf \
>>             -output-obj -o cpdflib.o \
>>             unix.cmxa bigarray.cmxa camlpdf.cmxa cpdf.cmxa cpdflib.cmx;
>> 	ar -x $(CAMLBASE)camlpdf/libcamlpdf_stubs.a;
>> 	ar -x $(CAMLBASE)ocaml/libasmrun.a;
>> 	ar cr cpdflib.a *.o
>>
>> test:   cpdflib.a cpdflibtest.c
>> 	cc cpdflibtest.c cpdflib.a -o test\
>>             -L $(CAMLBASE)ocaml -lbigarray -lunix
>>
>> clean:
>> 	rm __.SYMDEF\ SORTED *.o *.cmx *.cmi *.a test
>>
>>
>> (Here, cpdflib.ml, cpdflib.mli and cpdflibwrapper.c form the C interface
>> to the OCaml functions. cpdflibtest.c calls caml_startup and then any
>> caml functions it likes).
>>
>> This works. However, I'm having trouble getting rid of "-L
>> $(CAMLBASE)ocaml -lbigarray -lunix" -- i'd like the person linking with
>> my cpdflib.a not to need any dependencies at all.
>>
>> When I add two more lines
>>
>>           ar -x $(CAMLBASE)ocaml/bigarray.a;
>>           ar -x $(CAMLBASE)ocaml/unix.a;
>
> These are the wrong two libs. bigarray.a and unix.a are already
> contained in cpdflib.a. What you need are libbigarray.a and libunix.a.

Ah. A schoolboy error. Thanks!

However, it looks like this might not be possible anyway. In this thread...

http://caml.inria.fr/pub/ml-archives/caml-list/2007/11/c0ce27aa2dd792643615b6b705750657.en.html

...Alain Frisch comments on the method above, saying:

> Ok, that's a hack to merge one library, plus more object files into a
> library. I know it is mentioned in the OCaml manual. Personally, I would
> not recommend to do that (you'll be stuck if you need to include e.g.
> libunix.a).

So, unless I can find a way to get partial linking with ld -r to work, 
the best I have for now is:

CAMLBASE = /Users/john/.opam/4.00.1/lib/

mklib: cpdflib.mli cpdflib.ml cpdflibwrapper.c
	ocamlfind ocamlc -package cpdf cpdflib.mli;
	ocamlfind ocamlopt -package cpdf -c cpdflib.ml;
	ocamlfind ocamlc cpdflibwrapper.c;
	ocamlfind ocamlopt -I $(CAMLBASE)cpdf -I $(CAMLBASE)camlpdf \
           -output-obj -o cpdflib.o \
           unix.cmxa bigarray.cmxa camlpdf.cmxa cpdf.cmxa cpdflib.cmx;
	cp $(CAMLBASE)ocaml/libasmrun.a libcpdf.a;
	ar -x $(CAMLBASE)camlpdf/libcamlpdf_stubs.a;
	ar r libcpdf.a *.o

test:   libcpdf.a cpdflibtest.c
	cc cpdflibtest.c -o test -L. -lcpdf -lbigarray -lunix

clean:
	rm __.SYMDEF\ SORTED *.o *.cmx *.cmi *.a test

The end-user of the library still needs three -l options, but I need 
only ship the three .a files (libcpdf, libbigarray, libunix), which I 
suppose is not much harder on the end user than shipping one.

Obviously, I hope most people will be building from source, but I do 
need to ship binary libraries too.

Thanks,

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


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

end of thread, other threads:[~2013-08-27 13:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-22 17:34 [Caml-list] Building a mixed C / OCaml static library to be used from C John Whitington
2013-08-22 22:24 ` Gerd Stolpmann
2013-08-27 13:12   ` 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).