OCaml By Examples



A dune-project file is at the root of any project, it allows to set configuration for any executable or library in the project (there can be several).
(lang dune 2.8)

(implicit_transitive_deps false)


A dune allows you to declare an executable or a library using [S-expressions](https://en.wikipedia.org/wiki/S-expression) (basically parenthesis everywhere). Here we use dune to define an executable (not a library).
 (name main)
 (libraries core))


You find a `dune` file in the same folder as the library or executable files; that folder can be located anywhere in the folder containing the `dune-project` file and it can be named anything.
let () = print_endline "hello world"


You can use the `build` command line tool to build your project. The programs or library files will be generated and stored inside a `_build/` folder under your project's folder.
$ dune build main.exe
You can also directly run your program using `exec`. Note that the `.exe` extension has nothing to do with Windows executables.
$ dune exec main.exe
You can install `ocamlformat` (`opam install ocamlformat`) to auto-format your code, and the language server protocol `ocaml-lsp-server` to support clicking through function definitions in your favorite editor (check [OCaml Platform](https://github.com/ocamllabs/vscode-ocaml-platform) if you use VSCode).
$ opam install ocamlformat ocaml-lsp-server


Just note that you'll need an empty `.ocamlformat` file at the root for `ocamlformat` to work.
next: libraries