The compiler doesn’t really know about OPAM so it needs to be told where to find the libraries used by your program (in this case: graphics). Several ways to do it:
The old-school way:
$ ocamlc -I $(opam var graphics:lib) graphics.cma main.ml -o TEST
If you install ocamlfind using OPAM, you can alternatively do:
$ ocamlfind ocamlc -package graphics -linkpkg main.ml -o TEST
Finally, if you install dune using OPAM, you can also use it to build your project. You will need to create a file called dune-project next to main.ml containing
(lang dune 3.8)
and another file called dune containing
(executable
(name main)
(libraries graphics))
You can then build your program by typing dune build. The built executable will be available at the path _build/default/main.exe.
I can add that all added libraries must be linked with something like “nojb” has proposed, excepted the built in OCaml library (OCaml library). The open line doesn’t load any library. It just enable you to use a value from a loaded library without prefixing the module name.
Then, open Graphics won’t load Graphics, but if it is loaded, it will enable you to type open_graph instead of Graphics.open_graph. That’s it.
I tend to prefer prefixing my functions each time and avoid open. The main exception is probably Base or Core since they are designed as a replacement of Stdlib. I should add also Infix modules since they add some operators which would be too clumsy to prefix.