momoladebrouill
momoladebrouill

Reputation: 61

How to use installed module by opam in an ocaml script?

I want to use the Graphics modules that I installed on my machine with opam but it didn't recognized it, i have the output :

1 | let open Graphics in
             ^^^^^^^^
Error: Unbound module Graphics

when i run ocaml main.ml, even if my file starts with :

#use "topfind"

I'have tried reinstalling it multiple times, and i checked, it's correctly install in /home/me/.opam/default/lib/graphics. I'm using Debian with ocaml 4.14

Upvotes: 0

Views: 352

Answers (1)

octachron
octachron

Reputation: 18912

An opam package contains one (or sometimes more) libraries which themselves contain many modules (or sometimes only one module).

In the case of graphics, the opam package only contains the graphics library. To access the module of the library, you need to tell the REPL(toplevel) that you intend to use the graphics library with

#require "graphics";

then you will be able to use the Graphics module from the graphics library.

Note that outside of scripts, the information of which libraries to use should be part of the description of your project. If you are using dune (aka the recommended build system for OCaml), this means adding

(libraries graphics)

to your dune file.

Upvotes: 0

Related Questions