1

According to this "Error: unbound module" in OCaml

I should be able to run this

#use "topfind";;
#require "lwt";;
#require "cohttp-lwt-unix";;

as ocaml my_test1.ml

But after I've installed all the libraries and running it as such, I have an error:

$ ocaml my_test.ml 
Cannot find file topfind.
Unknown directive `require'.

update

$ ocaml my_test.ml 
File "./my_test.ml", line 1:
Error: Reference to undefined global `Mutex'

update2

#use "topfind";;
#require "lwt";;
#require "cohttp-lwt-unix";;
#thread;;

open Cohttp
open Lwt
open Cohttp
open Cohttp_lwt_unix

let () =
  Printf.printf ("test1")
;;

and

eval `opam config env`

ocaml test1.ml

same error:

File "./test1.ml", line 1:
Error: Reference to undefined global `Mutex'
Loku
  • 363
  • 1
  • 2
  • 7
  • 1
    WFM. Likeliest problem is that your environment isn't set up. Does `opam switch` warn you about that? – Julian Fondren Jan 05 '18 at 02:03
  • @JulianFondren, yes. But still not working. 1) After I've run ` eval `opam config env` ` and open a new terminal window, the environment gets restored to the previous state and I have to run ` eval `opam config env` ` again. Why? – Loku Jan 05 '18 at 02:36
  • 1
    sounds like A) my_test.ml doesn't contain the listed contents, and B) that you're missing a `#thread;;` – Julian Fondren Jan 05 '18 at 03:35
  • 2
    Just a small comment: when you do ``eval `opam config env` ``, it just sets things up in the shell in that particular window. You need to do it again for each window (or add the initialization to your shell startup files). – Jeffrey Scofield Jan 05 '18 at 04:34
  • @JulianFondren I've added "#thread;;" but the error remains – Loku Jan 05 '18 at 08:04
  • 1
    @Loku move the `#thread;;` above the line with the error. – Julian Fondren Jan 05 '18 at 16:13

2 Answers2

1

For some reason, the interpreter is picking the single-threaded runtime. Probably, there is some problem with your installation or it is a bug in ocamlfind. If you're using system OCaml installation, then my suggestion would be to switch to OPAM's compiler, e.g.,

opam switch 4.05.0

then install the necessary packages, e.g.,

opam install cohttp-lwt-unix 

and do not forget to activate your switch with

eval `opam config env`

If the problem still persists, then try to install another version of OCamlFind. If you still have a problem, then submit a bug report. The code you're showing should work (and works on mine machine).

ivg
  • 34,431
  • 2
  • 35
  • 63
1

ivg is right in that the interpreter is picking the single-threaded runtime, but that's something you have to fix in your application by adding #thread yourself:

#use "topfind";;
#thread;;
#require "lwt";;
#require "cohttp-lwt-unix";;

This is related to some recent changes in lwt and ocamlfind. You can find some pointers in this bug report I opened recently.

Étienne Millon
  • 3,018
  • 11
  • 27