How to fix the Unbound module Graphics in an ocaml project

From ~/pr/gitl/ocaml-gol

In a constant effort to learn new programming languages, I'm currently trying to use ocaml, a free and open-source general-purpose, multi-paradigm programming language maintained at the Inria. It's basically an extension of Caml with object-oriented features. I'm mostly interested by its functionnal and pattern matching features but the module part of the language can be a bit difficult to understand for someone with little to none ML(Meta Language) background.


 

The error

When trying to use the graphics module to create a graphical window and go just a little further than the simplest helloworld program, here is the result :

If the project uses dune :

(executable
  (name ocaml_project)
  (libraries lwt.unix graphics)
)

with this code :

let () =
  Printf.printf "Hello, world!\n";;
  Lwt_io.printf "Hello, world!\n";;
  Graphics.open_graph " 800x600";;

The first times I built this project running the dune build command, I got a Unbound module Graphics error and/or a Cannot find graphics.cma one.

Dependency installation

On Debian GNU/Linux, you can use the official package manager :

sudo apt install libgraphics-ocaml-dev liblwt-ocaml

On arch-based distributions, including manjaro, you'd better use the ocaml package manager, opam :

sudo pacman -S ocaml dune opam mercurial darcs patch
opam init
opam install lwt graphics

First, we install opam and needed dependencies using official arch package manager. It will install more than 500Mb of libraries. Then, we initialize it and finally, we install lwt and graphics ocaml modules.

You now should be able to build your project using the dune build command

Conclusion

Once built, you may run the binary from the ./_build/default/ocaml_gol.exe file. I'm not going further here, I'm just trying to learn ocaml (and other functionnal programming language like scheme, haskell or julia), but at least, this example can be compiled without any error.

Comments

Popular posts from this blog

How to make a map of variant in C++