0

I keep getting this error and don't understand why.

This is my OCaml code:

#load "graphics";;
open Graphics;;
open_graph "";;

let etoile x y c =
  moveto (x-c/2) (y-c/2) ;
  lineto (x-c/4) y ; 
  lineto (x-c/2) (y+c/2) ;
  lineto x (y+c/2) ;
  lineto (x+c/2) (y+c/2) ;
  lineto (x+c/4) y ;
  lineto (x+c/4) (y-c/4) ;
  lineto x (y-c/2) ;
  lineto (x-c/2) (y-c/2) ;;

etoile 100 100 20;;

and this is the terminal:

enter image description here

even though graphics are installed as you can see in the terminal.

glennsl
  • 28,186
  • 12
  • 57
  • 75
  • Have you used opam to install the graphics library? – Chris Apr 06 '23 at 00:23
  • After installing the Graphics library with `opam install graphics` try using `#require "graphics";;` – Chris Apr 06 '23 at 01:06
  • Does this answer your question? [OCaml Unbound Graphics Module](https://stackoverflow.com/questions/18435927/ocaml-unbound-graphics-module) – glennsl Apr 06 '23 at 09:39
  • Please don't post pictures of code or error messages. It makes the question unnecessarily inaccessible. – glennsl Apr 06 '23 at 09:40

2 Answers2

1

In recent versions of OCaml the graphics library is no longer in the standard library, and you would need to install it explicitly with opam. But as you are using an old version of OCaml, I think you should be OK without doing so.

However, as has been mentioned, if you are using the load directive then you need to load the relevant .cma or .cmo file, and in many cases you will also need to use the directory directive to add the directory in which it is situated to the search path. Better is to let ocamlfind deal with this for you, particularly if you have a more complicated set of modules than Graphics to load. This will do it for you:

#use "topfind"
#require "graphics"
Chris Vine
  • 677
  • 3
  • 7
0

It seems to me you have #load "graphics" but you should have #load "graphics.cma".

$ ocaml
        OCaml version 4.07.0

# #load "graphics";;
Cannot find file graphics.
# #load "graphics.cma";;
#
Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108