It is now relatively simple to create toploops. In order to include the packages p1,p2,..,pN simply execute the command
ocamlfind ocamlmktop -o toploop -package p1,p2,...,pN -linkpkg -custom
Toploops that already have "findlib" itself linked in support loading packages from scripts. This means that if a toploop is created by
ocamlfind ocamlmktop -o toploop -package findlib,p1,p2,...,pN -linkpkg -custom
it offers an additional directive "#require". This directive loads additional packages:
#require "q1,q2,...,qM"
"#require" loads the listed packages and all their ancestors in the right order, but leaves packages out that have already been loaded, either by a previous "#require" or by having them specified in the "ocamlmktop" command that created the toploop. Scripts can now simply document which packages must have been loaded by a "#require" directive right at the beginning of the script. Either these packages are already compiled into the toploop, or the packages are loaded at the moment the script is being executed.
For platforms that support DLLs, it is even possible to #require packages that have associated C libraries. Because it is not necessary to build custom toploops for these platforms at all, there is now a way to load findlib itself dynamically: Just invoke the directive
#use "topfind";;into the standard toploop "ocaml", and findlib is available! For a sample session, see the Quickstart page.
However, this does not work for platforms without DLL support. For these environments you must still specify the packages in the ocamlmktop command that link C libraries, and work with custom toploops.
Especially when developing packages, it is sometimes necessary to reload all dynamically loaded packages in the toploop. This can be forced by
Topfind.reset();;
which causes the "#require" directive to load all packages again.
Building of runtime systems is supported, too. For example, you can run
ocamlfind ocamlc -o runtime -make-runtime -package p1,p2,...,pN -linkpkg
but the problem is which options to specify when a program is linked for this runtime system. If you executed
ocamlfind ocamlc -o program -use-runtime runtime -package p1,p2,...,pN\ -linkpkg m1.cmo ... mM.cmo
it would be tried to link the archives from the packages again into the bytecode binary. Because of this, it is necessary to suppress linking in packages of the runtime system when linking binaries for a runtime system. The -dontlink option can be used for this:
ocamlfind ocamlc -o program -use-runtime runtime -package p1,p2,...,pN\ -dontlink p1,p2,...,pN -linkpkg m1.cmo ... mM.cmo
Note that the -package option can enumerate more packages than -dontlink, and in this case the additional packages are actually linked in as they are not contained in the runtime system.