Blame doc/guide-html/quickstart.html

Packit b513ef
Packit b513ef
<html><header><title>
Packit b513ef
QUICKSTART - The most important ways to use findlib</title></header>
Packit b513ef
<body bgcolor="white" text="black" >
Packit b513ef
Packit b513ef
                 cellspacing="0" border="0" cellpadding="10">
Packit b513ef
 
Packit b513ef
up
Packit b513ef
 
Packit b513ef

QUICKSTART - The most important ways to use findlib

Packit b513ef

Packit b513ef
Intro
Packit b513ef

Packit b513ef
See the file INSTALL for instructions how to build and install
Packit b513ef
findlib.
Packit b513ef

Packit b513ef
Findlib and the toploop
Packit b513ef

Packit b513ef
For a number of platforms, O'Caml can load bytecode-compiled
Packit b513ef
libraries dynamically. For these platforms, findlib is very simple to
Packit b513ef
use as explained in the following. For other platforms, see the paragraph
Packit b513ef
below about "custom toploops".
Packit b513ef

Packit b513ef
After the toploop has been started, it is possible to load the special
Packit b513ef
findlib support:[1]
Packit b513ef
Packit b513ef

Packit b513ef
$ ocaml
Packit b513ef
        Objective Caml version 3.07
Packit b513ef
Packit b513ef
# #use "topfind";;
Packit b513ef
Findlib has been successfully loaded. Additional directives:
Packit b513ef
  #require "package";;      to load a package
Packit b513ef
  #list;;                   to list the available packages
Packit b513ef
  #camlp4o;;                to load camlp4 (standard syntax)
Packit b513ef
  #camlp4r;;                to load camlp4 (revised syntax)
Packit b513ef
  #predicates "p,q,...";;   to set these predicates
Packit b513ef
  Topfind.reset();;         to force that packages will be reloaded
Packit b513ef
  #thread;;                 to enable threads
Packit b513ef
Packit b513ef
- : unit = ()
Packit b513ef

Packit b513ef
Packit b513ef
You can now list the available packages:
Packit b513ef
Packit b513ef

Packit b513ef
# #list;;
Packit b513ef
bigarray            (version: [distributed with Ocaml])
Packit b513ef
camlp4              (version: Camlp4 version 3.03 ALPHA)
Packit b513ef
dbm                 (version: [distributed with Ocaml])
Packit b513ef
dynlink             (version: [distributed with Ocaml])
Packit b513ef
findlib             (version: 0.6)
Packit b513ef
graphics            (version: [distributed with Ocaml])
Packit b513ef
labltk              (version: [distributed with Ocaml])
Packit b513ef
netstring           (version: 0.10)
Packit b513ef
num                 (version: [distributed with Ocaml])
Packit b513ef
stdlib              (version: [distributed with Ocaml])
Packit b513ef
str                 (version: [distributed with Ocaml])
Packit b513ef
threads             (version: [distributed with Ocaml])
Packit b513ef
unix                (version: [distributed with Ocaml])
Packit b513ef
xstrp4              (version: 1.1)
Packit b513ef

Packit b513ef
Packit b513ef
and load packages by simply typing:
Packit b513ef
Packit b513ef

Packit b513ef
# #require "netstring";;
Packit b513ef
Loading /opt/ocaml/lib/unix.cma
Packit b513ef
Loading /opt/ocaml/lib/str.cma
Packit b513ef
Loading /opt/ocaml/site-lib/netstring/netstring.cma
Packit b513ef
Loading /opt/ocaml/site-lib/netstring/netstring_top.cmo
Packit b513ef

Packit b513ef
Packit b513ef
Findlib takes care to load packages that are required by loaded packages
Packit b513ef
first. For example, "netstring" uses "unix" and "str" internally, but you
Packit b513ef
do not need to load them because findlib does it for you. In this example
Packit b513ef
you can also see that findlib loads netstring_top.cmo containing printers
Packit b513ef
for the toploop.
Packit b513ef

Packit b513ef
You can also enable the Camlp4 parsers by simply typing
Packit b513ef
Packit b513ef

Packit b513ef
# #camlp4o;;
Packit b513ef
Loading /opt/ocaml-3.03a/lib/camlp4/camlp4o.cma
Packit b513ef
        Camlp4 Parsing version 3.03 ALPHA
Packit b513ef

Packit b513ef
Packit b513ef
for the standard syntax or
Packit b513ef
Packit b513ef

Packit b513ef
# #camlp4r;;
Packit b513ef
Loading /opt/ocaml-3.03a/lib/camlp4/camlp4r.cma
Packit b513ef
        Camlp4 Parsing version 3.03 ALPHA
Packit b513ef

Packit b513ef
Packit b513ef
for the revised syntax. (But you cannot switch between the syntaxes.)
Packit b513ef
Packit b513ef

Packit b513ef
Custom Toploops
Packit b513ef

Packit b513ef
For some platforms, O'Caml does not implement loading external
Packit b513ef
libraries (e.g. Cygwin). One has to create a so-called custom toploop
Packit b513ef
that statically links with these libraries. Example:
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind ocamlmktop -o mytop -package findlib,unix -linkpkg
Packit b513ef
$ ./mytop
Packit b513ef
        Objective Caml version 3.07
Packit b513ef
 
Packit b513ef
# #use "topfind";;
Packit b513ef
Findlib has been successfully loaded. Additional directives:
Packit b513ef
  #require "package";;      to load a package
Packit b513ef
  #list;;                   to list the available packages
Packit b513ef
  #camlp4o;;                to load camlp4 (standard syntax)
Packit b513ef
  #camlp4r;;                to load camlp4 (revised syntax)
Packit b513ef
  #predicates "p,q,...";;   to set these predicates
Packit b513ef
  Topfind.reset();;         to force that packages will be reloaded
Packit b513ef
  #thread;;                 to enable threads
Packit b513ef
Packit b513ef
- : unit = ()
Packit b513ef

Packit b513ef
Packit b513ef
Now "#require" works for all libraries referring to the special "unix"
Packit b513ef
functions.
Packit b513ef
Packit b513ef

Packit b513ef
Findlib and scripts
Packit b513ef

Packit b513ef
The #require directive can also be used in scripts. Example:
Packit b513ef
Packit b513ef

Packit b513ef
#use "topfind";;
Packit b513ef
#require "netstring";;
Packit b513ef
Packit b513ef
open Cgi;;
Packit b513ef
...
Packit b513ef

Packit b513ef
Packit b513ef
This makes it possible to write scripts that do not contain #directory
Packit b513ef
directives that are specific for certain installations.
Packit b513ef

Packit b513ef
For Unix environments, you can start scripts directly if you
Packit b513ef
apply the following trick:
Packit b513ef
Packit b513ef

Packit b513ef
#! /bin/sh
Packit b513ef
# (*
Packit b513ef
exec ocaml "$0" "$@"
Packit b513ef
*) use "topfind";;
Packit b513ef
#require "netstring";;
Packit b513ef
Packit b513ef
open Cgi;;
Packit b513ef
...
Packit b513ef

Packit b513ef
Packit b513ef
This works wherever O'Caml is installed.
Packit b513ef

Packit b513ef
Compiling programs
Packit b513ef

Packit b513ef
Assumed you want to compile a program that uses the Netstring package.
Packit b513ef
Do it the following way:
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind ocamlc -package netstring -c myprogram.ml
Packit b513ef

Packit b513ef
Packit b513ef
This way you do not need to add "-I" options to locate Netstring.
Packit b513ef

Packit b513ef
If you want to create an executable, do not forget to add the
Packit b513ef
-linkpkg switch:
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind ocamlc -o myprogram -package netstring -linkpkg myprogram.cmo
Packit b513ef

Packit b513ef
Packit b513ef
This switch causes that the mentioned packages are added to the resulting
Packit b513ef
executable.
Packit b513ef

Packit b513ef
If you want to include several packages, you can either add several
Packit b513ef
"-package" options, or you can enumerate the packages separated by commas:
Packit b513ef
-package netstring,labltk.
Packit b513ef

Packit b513ef
Camlp4
Packit b513ef

Packit b513ef
If you add a -syntax option, the compiler will be told to parse the
Packit b513ef
source file using camlp4:
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind ocamlc -package netstring -syntax camlp4o -c myprogram.ml
Packit b513ef

Packit b513ef
Packit b513ef
Use -syntax camlp4o for the standard syntax or -syntax camlp4r for the
Packit b513ef
revised syntax.
Packit b513ef

Packit b513ef
Additionally, you can mention packages that add new syntax features.
Packit b513ef
The package xstrp4 is an example of this:
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind ocamlc -package xstrp4,netstring -syntax camlp4o -c myprogram.ml
Packit b513ef

Packit b513ef
Packit b513ef
Now you can use the $ notation that is implemented by xstrp4 in the
Packit b513ef
source file myprogram.ml.
Packit b513ef

Packit b513ef
Note that you can also invoke ocamldep from ocamlfind:
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind ocamldep -package xstrp4 -syntax camlp4o *.ml *.mli >.depend
Packit b513ef

Packit b513ef
Packit b513ef
This enables the syntax extensions, too.
Packit b513ef

Packit b513ef
ocamlbrowser
Packit b513ef

Packit b513ef
Since findlib-0.7, it is also possible to start ocamlbrowser from
Packit b513ef
ocamlfind. For example,
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind browser -package xstrp4
Packit b513ef

Packit b513ef
Packit b513ef
adds the correct path specification such that the modules contained in the
Packit b513ef
package xstrp4 are also displayed. With
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind browser -all
Packit b513ef

Packit b513ef
Packit b513ef
all package are added to the path spec.
Packit b513ef
Packit b513ef

Packit b513ef
The Makefile wizard
Packit b513ef

Packit b513ef
There is a wizard that makes it very easy to write Makefiles. Call the
Packit b513ef
wizard by
Packit b513ef

Packit b513ef
$ ocamlfind findlib/make_wizard
Packit b513ef

Packit b513ef
(the wizard requires that the labltk library is available). A new window
Packit b513ef
pops up, and by very few clicks you can describe your own library. Finally,
Packit b513ef
a Makefile is written.
Packit b513ef
Packit b513ef

Packit b513ef
There is no magic!
Packit b513ef

Packit b513ef
Findlib is neither a patch of O'Caml nor uses it internal features of
Packit b513ef
the O'Caml programming environment. It is only a convention to install
Packit b513ef
software components in filesystem hierarchies, a library interpreting 
Packit b513ef
this convention, and some frontend applications making the library useable for
Packit b513ef
you.
Packit b513ef

Packit b513ef
One important consequence is that you can only refer to those
Packit b513ef
software components that have previously been installed in a way findlib
Packit b513ef
understands. This convention is beyond the scope of this QUICKSTART guide,
Packit b513ef
see the reference manual for details. You can always check whether findlib
Packit b513ef
accepts a component as "findlib package" by the command
Packit b513ef
Packit b513ef

Packit b513ef
$ ocamlfind list
Packit b513ef

Packit b513ef
Packit b513ef
(this is the same as the #list directive in the toploop). If the package
Packit b513ef
occurs in the list, it is found, otherwise not.
Packit b513ef


Packit b513ef
Packit b513ef
[1]
Packit b513ef
In previous versions, #use "findlib" loaded the
Packit b513ef
library. However, this caused a name conflict for a certain type of
Packit b513ef
installation. Because of this, the name of the loader script has been changed
Packit b513ef
to "topfind", but "findlib", and "ocamlfind" (Debian) are also available
Packit b513ef
for backwards compatibility.
Packit b513ef
Packit b513ef

Packit b513ef
                 cellspacing="0" border="0" cellpadding="10">
Packit b513ef
 
Packit b513ef
up
Packit b513ef
 
Packit b513ef
</html>