Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Created April 9, 2019 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jtpaasch/ce364f62e283d654f8316922ceeb96db to your computer and use it in GitHub Desktop.
Save jtpaasch/ce364f62e283d654f8316922ceeb96db to your computer and use it in GitHub Desktop.
Notes on using `dune` for OCaml.

Dune

A first pass

A library and then an executable that uses that library.

A simple library

Create a folder called foo:

mkdir foo
cd foo

Create a file called foo.ml with these contents:

let greeting name = Printf.printf "Hello, %s\n%!" name

Create a foo.opam file with these contents:

opam-version: "2.0"
name: "foo"
version: "0.1"
synopsis: "One-line description"
description: """
Longer description
"""
maintainer: "Name <email>"
authors: "Name <email>"
license: "MIT"
homepage: "https://foo.com"
bug-reports: "bugs@foo.com"
dev-repo: "git+https://foo.com"

Create a dune file with these contents:

(library
  (name foo)
  (public_name foo))

Create a Makefile with these contents:

all: build.local

clean.local:
        opam pin remove -y .
        dune uninstall
        dune clean

build.local:
        dune build
        opam pin add -y .

Build it:

make

Confirm that opam and ocamlfind can find it:

opam find | grep foo
ocamlfind query foo

Confirm you can use it. Open utop, then:

#use "topfind";;
#require "foo";;
Foo.greeting "Jorge!";;
#quit;;

Create a .gitignore file, with these contents:

_build
.merlin

Initialize and save to git:

git init
git add -A
git commit -m "Initial commit."

An executable that uses the library

Create another folder, called foozball:

cd ..
mkdir foozball
cd foozball

Create a file called foozball.ml with these contents:

Foo.greeting "Jorge.";

Create a dune file:

(executable
  (name foozball)
  (libraries foo))

Create a Makefile:

exe = foozball.exe

all: clean build

clean:
        dune clean

build:
        dune build $(exe)

Create a .gitignore file:

_build
*.install
.merlin

Build it:

make

Try it out:

_build/default/foozball.exe

Add everything to git and commit:

git init
git add -A
git commit -m "Initial commit."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment