OCaml Project Setup Guide
We’re going to get everything you need to install dependencies, compile, link, and run OCaml programs. There are many different tools that do these things, but in this course we will use OPAM and ocamlbuild.
The package manager OPAM lets us install and use community libraries easily. It also manages the OCaml compiler and standard library, making it simple to change compiler/dependency versions in lock-step. It is the first thing you need to be running OCaml programs.
Installing OPAM
OPAM is available for Linux, OSX, and (somewhat weakly via Cygwin) Windows. I (your TA) recommend Windows users to use campus Linux resources or a virtual machine rather than Cygwin, mainly because I will be less helpful than Google if things go wrong.
The installation guide is here: OPAM Install Guide. Once you have OPAM installed, open up a terminal and cd your way to the directory where your code is. I will assume the code you are trying to compile and run is named test.ml. For multiple files, keep reading.
Configuring OPAM
> ls |
test.ml |
> opam switch 4.03.0 |
> eval `opam config env` |
> ocamlbuild test.native |
> ls |
test.native test.ml |
> ./test.native |
... any output from your program |
> opam switch 4.03.0 |
> eval `opam config env` |
> ocamlbuild test.native |
> ocamlbuild -use-ocamlfind test.native |
Here is the content of an example project that uses external dependencies and multiple files in different directories.
> ls -R |
.: |
src _tags |
|
./src: |
lang test.ml |
|
./src/lang: |
lc.ml |
> cat _tags |
<src> : include |
<src/lang> : include |
true : package(oUnit, ocamlgraph) |
> ocamlbuild -use-ocamlfind test.native |
> ./test.native |
<src> : include |
<src/lang> : include |
true : package(oUnit, ocamlgraph) |