On this page:
Installing OPAM
Configuring OPAM
6.8

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.04.0

> eval `opam config env`

> ocamlbuild test.native

> ls

test.native test.ml

> ./test.native

... any output from your program

Line by line:

> opam switch 4.04.0

This will switch the OCaml compiler, build tools, and packages to version 4.04.0. You may have already done this in the installation process. You only have to do this once. We will be using OCaml version 4.04.0 for this course, any work that is incompatible with this version will be graded harshly.

> eval `opam config env`

This call to eval sets up your environment variables and adds the necessary executables to your path. You must run this before you can compile and run OCaml code. I recommend adding it to your shell’s init script.

> ocamlbuild test.native

This compiles the test.ml file into an executable named test.native. This assumes that test.ml does not reference any packages installed by OPAM. To include packages installed by OPAM, you must call:

> ocamlbuild -use-ocamlfind test.native

and have a properly configured _tags files. A _tags file in the top-level directory of a project will change how ocamlbuild behaves. We will use it to include dependencies installed by OPAM and include other directories of local source code.

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

Line by line:

<src> : include

<src/lang> : include

This _tags file includes any code in the subfolders src and src/lang. The top-level directory (which contains the _tags file) is always included. To traverse all subdirectories without explicitly including them in the _tags file, you can call ocamlbuild -r -use-ocamlfind test.native instead.

true : package(oUnit, ocamlgraph)

This _tags file also includes ounit and ocamlgraph, external packages installed by opam. For more information on _tags file configuration, look here: ocamlbuild user guide.

 

Web Accessibility