Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
RE: [Caml-list] C libs from Ocaml libs
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-11-22 (16:42)
Subject: RE: [Caml-list] C libs from Ocaml libs
Hello Viktor,

I think that some libraries are missing in the link phase.


When a x.cmxa is generated, a x.lib is also generated. You need to pass it to the linker when you are generating a c executable or dll, even if you use the .cmxas in your output-obj, because all the relevant code is not just there.


I am appending the makefile of some internal code that generated a C Win32 dll.


Hope this helps.



DIRS=-w y -w p -I +camljava -I +gsl -I +schedule -I +ocamlDM -I ../common/        

LIBS=rschedule.cmxa ocamlDMManager.cmxa jni_init.cmxa common.cmxa 

all: Engine.dll


install: all

            cp Engine.dll ../../bin


# supposes that the linker has access to the j2sdk lib

# rabih /libpath:"d:/appli/j2sdk1.4.1_05/lib" 

Engine.dll: ocamllib.obj Engine.obj

            link /nologo /dll /out:Engine.dll \

            /libpath:$(OCAML_LIB) /libpath:$(OCAML_CAMLJAVA) /libpath:$(JDKHOME)/lib \

            /libpath:$(OCAML_LIB)/schedule /libpath:$(OCAML_LIB)/ocamlDM /libpath:$(OCAML_LIB)/gsl /NODEFAULTLIB:libc \

            jvm.lib jni.lib  rschedule.lib ocamlDMManager.lib \

            Engine.obj  ocamllib.obj \

            libasmrun.lib libcamljni.lib


Engine.obj: Engine.h Engine.c

            cl /c $(JAVA_INC) -I$(OCAML_LIB) Engine.c




            ocamlopt $(DIRS) -c -dtypes $<



ocamllib.obj:ocaml _Engine.cmx

            ocamlopt -dtypes $(DIRS) -output-obj -o ocamllib.obj  $(LIBS) $^



            echo -n > .depend

            ocamldep $(DIRS) >.depend



            rm -f *.obj

            rm -f *.dll

            rm -f *.lib

            rm -f *.exp

            rm -f *.cm*

            rm -f *.h

            rm -f *~


include .depend

include ../Makefile.config



De : [] De la part de viktor tron
Envoyé : mercredi 21 novembre 2007 19:35
À : Caml Mailing List
Objet : Re: [Caml-list] C libs from Ocaml libs



On 21/11/2007, Alain Frisch < > wrote:

viktor tron wrote:
> This is super! how is this on MacOS, I recall one of your comment earlier (on this list?)
> that it doesn't work or something.

Everything should work fine under Mac OS X x86. For PowerPC, I believe 
that "ocamlc -output-obj -o" should be ok but that "ocamlopt
-output-obj -o" does not work.

> well, I tried.
> ar -rs foo_caml.o foo_stub.o
> gcc -o foo_test.native foo_test.c -L. -lchainfreq_native -L/sw/lib/ocaml 

What is the name of the library you want to produce?

sorry, let me be very explicit then:

I believed that the following 
creates the c object containing: 
(a) main ocaml implementation of foo (b) export API (c) C binding to API (d) ocaml startup code 

$ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c

now I create a lib

$ ar rs libfoo.a foo_caml.o

and happy ever after. Nope.
No matter how I link it with a main c test, I get undefined symbols for startup code 

$ gcc -o foo_test foo_test.c -L. -lfoo -lasmrun

In fact, the following variants don't work:

$ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c
$ ar -rs libfoo.a /sw/lib/ocaml/libasmrun.a foo_caml.o 
$ gcc -o foo_test foo_test.c -L. -lfoo

$ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c
$ cp /sw/lib/ocaml/libasmrun.a libfoo.a 
$ ar -rs libfoo.a foo_caml.o 
$ gcc -o foo_test foo_test.c -L. -lfoo 

so it seems I have to compile foo_stub.o separately...

but even if I do:

$ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c
$ ocamlopt foo_stub.c
$ ar -rs libfoo.a foo_caml.o foo_stub.o 
$ gcc -o foo_test foo_test.c -L. -lfoo -L/sw/lib/ocaml -lasmrun

$ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c
$ ocamlopt foo_stub.c
$ ar -rs libfoo.a /sw/lib/ocaml/libasmrun.a foo_caml.o 
$ gcc -o foo_test foo_test.c -L. -lfoo

both fail, the only thing that works is when
(a) I compile stub and caml objects separately AND 
(b) use copy to bootstrap the runtime

$ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c
$ ocamlopt foo_stub.c
$ cp /sw/lib/ocaml/libasmrun.a libfoo.a 
$ ar -rs libfoo.a foo_caml.o foo_stub.o
$ gcc -o foo_test foo_test.c -L. -lfoo

In fact ocamlopt does pretty enigmatic things in the background and ar is an entire mystery with this 
copy thing. 
If anyone can make me understand the above, I'd be forever grateful.



Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. 
Toute utilisation ou diffusion non autorisee est interdite. 
Tout message electronique est susceptible d'alteration. 
Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. 
Decouvrez l'offre et les services de Societe Generale Asset Management sur le site 
This message and any attachments (the "message") are confidential and intended solely for the addressees. 
Any unauthorised use or dissemination is prohibited. 
E-mails are susceptible to alteration. 
Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified. 
Find out more about Societe Generale Asset Management's proposal on