The findlib User's Guide
Prev | Chapter 2. Using packages | Next

# How to compile and link a program that uses a package

Now suppose you want to compile a program which calls functions of your new package p. If prog1.ml, prog2.ml, and prog3.ml are the three source files the program consists of, compile them with the commands

    ocamlfind ocamlc -package p -c prog1.ml
    ocamlfind ocamlc -package p -c prog2.ml
    ocamlfind ocamlc -package p -c prog3.ml

The "ocamlfind ocamlc" invocation is a frontend to "ocamlc". Most arguments are directly passed to "ocamlc", but there are a few new options, and often some new options are implicitly added. Here, the new -package option is used, which adds search paths such that the modules of package p are found. Effectively, the following direct ocamlc invocations would be equivalent [1]:

    ocamlc -I /usr/local/lib/ocaml/site-lib/p -c prog1.ml
    ocamlc -I /usr/local/lib/ocaml/site-lib/p -c prog2.ml
    ocamlc -I /usr/local/lib/ocaml/site-lib/p -c prog3.ml

The -I option has the effect that the named directory is also searched when looking up cmi files. Because of this you can refer directly to the modules M1 and M2 in the program sources.

In order to link the program use the following command:

    ocamlfind ocamlc -o program -package p -linkpkg prog1.cmo prog2.cmo prog3.cmo

The -linkpkg option causes some more arguments to be added to the constructed ocamlc command. Especially, the name of the archive of p is extracted from the META file, and automatically inserted before the prog1.cmo argument. The resulting command looks like[2]:

    ocamlc -o program -I /usr/local/lib/ocaml/site-lib/p p.cma prog1.cmo prog2.cmo prog3.cmo

Please note that the bytecode archive p.cma has been selected, and not the native archive p.cmxa. As it is known that the bytecode compiler is used, the predicate "byte" is automatically set.

### Notes

[1] | If you specify the -verbose option, the constructed command is printed to the terminal. Actually, there are some more implicitly added options, especially -ccopt -I<dir> for every package directory <dir>. This means that you can compile C programs accessing header files stored in the package directory.
[2] | Again, the actual command contains even some more arguments...

Prev | Home | Next
Querying information stored in META files | Up | Dependencies