Chapter 3. Dependency analysis of packages

Every package denotes in its META file only the list of direct
ancestors. The theoretical model of the dependency relation is a
directed acyclic graph (DAG), with the packages as vertices and edges
from packages to their direct ancestors. The graph must be acyclic
because OCaml does not allow cyclic dependencies between modules.

What happens if you query something like

ocamlfind query -recursive p1 p2 ... pN

is that the named packages p1 to pN are marked in the graph, and that
repeatedly all direct ancestors of marked packages are marked, too,
until there is not any marked package remaining with an unmarked
ancestor. All marked packages are then printed in topological
order. This simply means that for the printed packages p1 to pM holds
that if pI is printed before pJ then pI is a (possibly indirect)
ancestor of pJ.

The topological order plays a role when the link command is
constructed by "ocamlfind ocamlc", as Ocaml requires that archives
must be linked in topological order. For example, the link statement

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

must be turned into the effective command

ocamlc -o another [...more options...] p.cma q.cma another.ml

and not

ocamlc -o another [...more options...] q.cma p.cma another.ml

because there are references from q.cma to p.cma.

In C, there is a similar requirement when linking static archives. The
linker backend ld wants the archives in reversed
topological order, i.e. the deepest ancestor must come last in the
list of linked archives. Because of this, the additional linker
options specified in the "linkopts" variable are passed in reversed
order to the underlying linker. This means that you can refer to C
libraries of ancestor packages of p in C libraries provided in p.

Note that most operating systems do not require any specific order for
dynamically linked C libraries (the exception is, surprise!, AIX).