Re: Linking caml generated object in NT

From: Xavier Leroy (
Date: Fri Oct 23 1998 - 11:30:49 MET DST

Date: Fri, 23 Oct 1998 11:30:49 +0200
From: Xavier Leroy <>
To: Friedman Roy <>,
Subject: Re: Linking caml generated object in NT
In-Reply-To: <Pine.GSO.3.95-heb-2.07.981020152420.5796C-100000@csa>; from Friedman Roy on Tue, Oct 20, 1998 at 03:29:08PM +0200

> I am trying to link object files generated by ocamlopt with object files
> generated by the C compiler (cl for instance) and got a lot of unknown
> symbols errors. I tried adding some or all of the libraries in the lib
> directory, and still a few symbols remained unknown. Is there a list of
> libraries one has to link with to make this work.

The main difficulty is that some glue code is generated at link-time
when you lin with ocamlopt, so in general you can't just link together
the .obj files produced by the ocamlopt compiler using another linker.

Your best bet is to use the "-output_obj" option to build a
self-contained .lib file with all your compiled Caml code + the glue
code generated at link-time + the OCaml runtime library:

        ocamlopt -output_obj -o mycamlcode.lib <the .cmx and .cmxa files
                                                 for your program>

Then, you need to provide a main() function in C to start the Caml
code. The following should do:

int main(int argc, char ** argv) {
        return 0;

Finally, use the NuMega linker to link together mycamlcode.lib,
the C libraries used by your Caml code, and the main() function above.

See the "Embedding the Caml code in the C code" and "Main program in C"
sections of the chapter "Interfacing C with Objective Caml" of the manual
for more details.

I haven't tried -output_obj under Windows in a long time, so the above
might not be 100% accurate, but I hope it will help.

- Xavier Leroy

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:16 MET