0

Here is a simple ocaml file, just meant to get me to understand how to load a program that uses the Batteries library into the ocamltop.

batteryassault.ml

open Batteries

let main = print_string "hello, world ... powered on"

I compile this to bytecode with

ocamlfind ocamlc -package batteries -linkpkg batteryassault.ml

Resulting in batteryassault.cmo and batteryassault.cmi, and no errors nor warnings. Then, I start the battery-powered ocamltop with

rlwrap ocamlfind batteries/ocaml

Finally, to load the file in ocamltop:

#load "batteryassault.cmo" ;;

And then we get an error.

The files batteryassault.cmo and /usr/lib/ocaml/batteries/batteries.cma disagree over interface Batteries

I think what could be going on is that Ubuntu installs batteries 2.2.1, but for some reason (installing merlin?) I have batteries 2.3.1 installed in my opam folder, and moreover, when starting the ocamltop with batteries as above, it indicates that ocamltop is using the 2.2.1 version. Further, compiling with

ocamlfind ocamlc -package batteries -linkpkg batteryassault.ml -verbose

I find that ocamlc is definitely using the library in opam, i.e. the 2.3.1 version.

So my question is: what is the/is there a workaround?

Jonathan Gallagher
  • 2,115
  • 2
  • 17
  • 31

1 Answers1

0

Usually I suggest to remove system OCaml installation at all. Otherwise you may have such errors.

You should ensure, that all your tools and libraries come from the same source, in this particular case from opam.

A usual error is to forget to call for

eval `opam config env`

at some point. You may recall later about this, and activate your opam, thus ending up, with parts of your application, or even opam stack built using opam with other parts built with system libraries. If you made such error, it will be hard to guess, when and what goes wrong.

But if you still don't want to remove system batteries, then you need to be very careful. For example, since ocamlfind batteries/ocaml results in loading old version of batteries, then you can try to use this trick, to make sure that batteries topleve is loaded from opam:

$ `opam config var batteries:lib`/ocaml

If the problem persists, that means that your system is really broken. Consider removing you opam stack and making carefully everything from scratch. E.g.,

 rm -rf ~/.opam
 opam init --comp=4.02.1
 eval `opam config env`
 opam install batteries
ivg
  • 34,431
  • 2
  • 35
  • 63