3

When loading the Graphics module in the toplevel, I get an error saying "Cannot find graphics.cma".

I'm using OS X and I'm pretty sure I've installed OCaml correctly since I've been using it for about a month now. So it seems that the Graphics module wasn't included in the OCaml package.

How can I fix this issue, or how can I install the Graphics module myself?

Thomas Vanhelden
  • 879
  • 8
  • 20
  • How did you install OCaml? My OS X install through homebrew has graphics.cma. But I haven't updated it in quite a while; I'm still running OCaml 4.01.0. – Jeffrey Scofield Mar 20 '15 at 19:35

2 Answers2

6

The solution mostly depends on how are you compiling. If you're using ocamlbuild tool, the the following would be sufficient:

ocamlbuild -use-ocamlfind -pkg graphics my_program.native

Where my_program.native is your target. Note, you can try to omit the -use-ocamlfind flag.

In order to bring graphics to your toplevel, the easiest solution would be to use topfind:

# #use "topfind";;
# #require "graphics";;
ivg
  • 34,431
  • 2
  • 35
  • 63
  • 1
    Thanks, just spent quite some time chasing wrong answers trying to make the first tutorial example work, after having installed everything trough opam, and finally this did the trick. Talk about ocaml being a mature and industry-ready language... – dividebyzero Jan 11 '20 at 16:34
5

First of all, check Graphics is really installed. This library is optional and therefore it may not be installed. There are several ways to check but the following should work for any situation:

$ ls `ocamlc -where`/graphics*

If there is no file listed by the above command, Graphics is not installed and you have to reinstall OCaml compiler enabling Graphics.

If files like graphics.cma are there, then you have to show us how you try to compile your code with Graphics. The best answer varies depending on how you compile: inside toplevel, hand compiling with ocamlc, or with some build tool like ocamlbuild.

camlspotter
  • 8,990
  • 23
  • 27