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

# Dependencies

Often packages use other packages themselves. Let q be another package consisting of the single module M3 which contains references to M1 and M2. At the first glance, we can ignore this when describing the package, as we could always add "-package p" and "-package q" options when compiling programs using q. This solution is not optimal, as the user of a package must have knowledge about details of the package that should normally be hidden. When we introduced the notion of packages, one of the most important properties was that we can replace packages by improved versions. Imagine that q is replaced by q', but q' uses not only p but also r. Every program that used q and is now forced to use q' must be changed (at least in the Makefile) in order to link r, too. This is clearly not what is intended by packages.

The far better solution is to store dependency information in the META files. The "requires" variable can list names of packages that are direct ancestors, i.e. referred directly. (Do not put indirect ancestors into this variable, for example further packages required by r when describing q - these are found out automatically.) The META file of q looks like:

    description = "Something that needs p"
    requires = "p"
    version = "1"
    archive(byte) = q.cma
    archive(native) = q.cmxa

If you want to put several package names into "requires", separate them with commas or spaces.

The "ocamlfind query" command ignores the "requires" value by default, you must add the -recursive option. In this case, all direct or indirect ancestors of the packages given on the command line are selected, too, and printed in topological order. For example, the command

    ocamlfind query -recursive -long-format -predicate byte q

prints two records:

    package:     p
    description: Our super-duper package
    version:     1
    archive(s):  p.cma
    linkopts:
    location:    /usr/local/lib/ocaml/site-lib/p

    package:     q
    description: Something that needs p
    version:     1
    archive(s):  q.cma
    linkopts:
    location:    /usr/local/lib/ocaml/site-lib/p

Without -recursive, only q would have been printed.

The compiler frontend provided with ocamlfind always works recursively. In order to compile and link another.ml that uses q, the following command is sufficient:

    ocamlfind ocamlc -o another -package q -linkpkg another.ml

It is not necessary to specify -package p in this statement as the dependency relation is always used to find out the actually meant set of packages.

Prev | Home | Next
How to compile and link a program that uses a package | Up | Linker options