4

I followed the instructions here for setting installing a library in site-lib using ocamlfind install. I had two libraries: one called logic and another called boolean. In each case I installed the .cmo, .cmx, .cmi and .mli files in the library, for example:

ocamlfind install boolean META boolean.cmo boolean.cmx boolean.cmi boolean.mli

Then when I went to build another project that depends on logic and boolean using ocamlbuild, I got the following error message:

$ocamlbuild -use-ocamlfind test_logic.native -classic-display
...
/home/phil/godi-3.12.1.0/bin/ocamlfind ocamlopt -c -package boolean -package deriving-    ocsigen -package deriving-ocsigen.syntax -package logic -package oUnit -package unix -syntax   camlp4o -o test_logic.cmx test_logic.ml
/home/phil/godi-3.12.1.0/bin/ocamlfind ocamlopt -linkpkg -linkpkg -package boolean -package deriving-ocsigen -package deriving-ocsigen.syntax -package logic -package oUnit -package unix -syntax camlp4o vhdl.cmx fsm.cmx test_logic.cmx -o test_logic.native
+ /home/phil/godi-3.12.1.0/bin/ocamlfind ocamlopt -linkpkg -linkpkg -package boolean -package deriving-ocsigen -package deriving-ocsigen.syntax -package logic -package oUnit -package unix -syntax camlp4o vhdl.cmx fsm.cmx test_logic.cmx -o test_logic.native
gcc: /home/phil/godi-3.12.1.0/lib/ocaml/site-lib/logic/logic.o: No such file or directory
gcc: /home/phil/godi-3.12.1.0/lib/ocaml/site-lib/boolean/boolean.o: No such file or directory
File "caml_startup", line 1, characters 0-1:
Error: Error during linking
Command exited with code 2.

I then copied the .o files from the logic and boolean projects over to their respective areas in site-lib and it compiled and linked fine.

I'm wondering why the .o files were needed and why gcc is involved here?

Here's my _tags file in case it helps:

<*.ml> or "test_logic.native" or "test_loginc.byte": package(boolean),package(unix),     package(oUnit), package(deriving-ocsigen), package(deriving-ocsigen.syntax), syntax(camlp4o), package(logic)
Community
  • 1
  • 1
aneccodeal
  • 8,531
  • 7
  • 45
  • 74
  • I don't know the details, however ocamlopt is a native compiler. It generates both a .o file and a .cmx file. They need to be kept together as a pair. ocamlopt does linking by running a linker internally, and often it uses gcc as the linker. You can see the commands it runs with the -verbose flag. – Jeffrey Scofield Apr 25 '12 at 06:18
  • shouldn't the .cmx files in the library (in site-lib/logic and site-lib/boolean) be enough? – aneccodeal Apr 25 '12 at 06:35
  • Well, ocamlopt doesn't work that way. The .o file contains all the generated code. The .cmx file is just meta-information. This is useful because you can just use standard Unix tools on the .o files. For example, you don't need a special linker. – Jeffrey Scofield Apr 25 '12 at 06:38

1 Answers1

4

See Section 11.1 of the OCaml Manual:

From the file x.ml, the ocamlopt compiler produces two files: x.o, containing native object code, and x.cmx, containing extra information for linking and optimization of the clients of the unit. The compiled implementation should always be referred to under the name x.cmx (when given a .o or .obj file, ocamlopt assumes that it contains code compiled from C, not from Caml).

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108