OCaml By Examples



You can use dune to manage internal (as well as external) libraries. You'll just need a name and a list of dependencies (in the `libraries` field).
 (name lib)
 (libraries core))


open Core

let add_one i = i + 1


You can build your library using dune.
$ dune build
The built library will reside under `_build/default/`. Relevant files are `.cma` (bytecode), `.cmxa` (native), and `.cmi` (the library's interface)
$ ls _build/default/
dune         lib.cma      lib.cmxs
lib.a        lib.cmxa     lib.ml
You can use dune to launch utop with your library.
$ dune utop .

utop # Lib.add_one 1;;
- : int = 2


You can also import all the code from a file by including directly in utop.
utop # #use "lib.ml";;
val add_one : int -> int = <fun>

utop # add_one 1;;
- : int = 2
You can also import a file as a module.
utop # #mod_use "lib.ml";;
module Lib : sig val add_one : int -> int end

utop # Lib.add_one 2;;
- : int = 3
next: tests