Alexander Kleinhans
Alexander Kleinhans

Reputation: 6248

How to get missing modules in Ocaml?

[Solved (at bottom). installed quartz and re-installed with x11 via brew then restarted machine.]

I'm learning Ocaml and am going through these documentations pages and need to install some modules (graphics).

I'm missing a Graphics module in Ocaml. After trying to load it on toplevel (the REPL right?) with:

$ ocaml
       OCaml version blahblah
# #load "graphics.cma";;
# open Graphics;;

I get the error message:

Cannot find file graphics.cma.

So I wander over to this question and after not finding the file with the command:

ls `ocamlc -where`/graphics*` 

I read that this means that:

Graphics is not installed and you have to reinstall OCaml compiler enabling Graphics.

Does this mean I have to recompile Ocaml every time I need a new module? I'm not sure what he meant by that.

Then, I tried to install Graphics with: opam install graphics.

I got this error:

This package relies on external (system) dependencies that may be missing. `opam depext lablgl.1.05' may help you find the correct installation for your system.

So I did opam depext lablgl.1.05

After this, I tried opam install graphics again, but it failed with this error:

#=== ERROR while installing graphics.1.0 ======================================#
# opam-version 1.2.2
# os           darwin
# command      ocamlc -custom graphics.cma -o test
# path         /Users/alexanderkleinhans/.opam/system/build/graphics.1.0
# compiler     system (4.02.2)
# exit-code    2
# env-file     /Users/alexanderkleinhans/.opam/system/build/graphics.1.0/graphics-24451-7afd23.env
# stdout-file  /Users/alexanderkleinhans/.opam/system/build/graphics.1.0/graphics-24451-7afd23.out
# stderr-file  /Users/alexanderkleinhans/.opam/system/build/graphics.1.0/graphics-24451-7afd23.err
### stderr ###
# File "_none_", line 1:
# Error: Cannot find file graphics.cma



=-=- Error report -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
The following actions failed
  ∗  install graphics 1.0
No changes have been performed

=-=- graphics.1.0 troubleshooting -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
=> This package checks whether the Graphics library was compiled.

This error says Cannot find file graphics.cma which brings me back to this question and what the steps are to get graphics.cma (and other modules as I might need them).

I though opam was a package manager for ocaml (this install modules right?)

EDIT:

I did brew info ocaml and I did install with x11 so I though this meant I should have it...

ocaml: stable 4.04.1 (bottled), devel 4.05.0+beta3, HEAD
General purpose programming language in the ML family
https://ocaml.org/
/usr/local/Cellar/ocaml/4.04.1 (1,730 files, 194.4MB) *
  Poured from bottle on 2017-06-13 at 15:23:43
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/ocaml.rb
==> Requirements
Optional: x11 ✘
==> Options
--with-flambda
    Install with flambda support
--with-x11
    Install with the Graphics module
--devel
    Install development version 4.05.0+beta3
--HEAD
    Install HEAD version

EDIT 2:

Running

brew install Caskroom/cask/xquartz
brew reinstall ocaml --with-x11

Allowed me to compile, but running gave me this fatal exception. Seems to be an X11 thing?

Fatal error: exception Graphics.Graphic_failure("Cannot open display ")

Solved

So I think the two steps that were necessary were to make sure ocaml was installed with X11. Note that `brew info ocaml` seemed to give wrong information (said it was installed with X11 but reinstall was necessary). On OSX, I also needed to install quarts.

brew install Caskroom/cask/xquartz
brew reinstall ocaml --with-x11

After this I COULD compile, but got an error on execution. This was simply solved by restarting which I read was necessary after installation of xquartz.

After that I could run fine.

Upvotes: 0

Views: 1786

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66803

The graphics module is an optional part of the base OCaml install, not an external module. This explains why you can't install it using OPAM. The OPAM module that you show is only testing whether it is installed in the current OCaml system. It can't (and hence doesn't try to) install graphics as a separate module.

For this reason, installing graphics (when it's not already installed) is unusually tricky. There's no need to recompile OCaml for installing most (if not all) other modules.

For what it's worth, I am running macOS 10.12.4, and I used "opam switch" to switch my OCaml system to the 4.03.0 release. In the resulting environment, the Graphics module is installed, and I have no trouble running the examples at the website you mention. (For the first, I see concentric red and yellow circles, for example.)

You might try doing "opam switch" to switch to a recent version of the compiler, and see if this gets things going for you. In the past I have had trouble getting Graphics to work, but it is working great for me now.

Upvotes: 3

Related Questions