7

I'm trying to make this piece of code:

open Lwt;;
open Cohttp;;
(* a simple function to access the content of the response *)
let content = function
  | Some (_, body) -> Cohttp_lwt_unix.Body.string_of_body body


(* launch both requests in parallel *)
let t = Lwt_list.map_p Cohttp_lwt_unix.Client.get
  (List.map Uri.of_string
     [ "http://example.org/";
       "http://example2.org/" ])

(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content

(* launch the event loop *)
let v = Lwt_main.run t2

However, when i run

Ocamlbuild file.native

I get unbound module errors.

These modules were installed via opam and when I run

ocamlfind query lwt 
/home/chris/.opam/system/lib/lwt
ocamlfind query cohttp
/home/chris/.opam/system/lib/cohttp

How do I get Ocamlbuild to find these two packages?

I have tried

Ocamlbuild -pkgs cohttp,lwt file.native 

and it did not work. It said something about a possibly incorect extension. I don't think that is the problem though.

If anyone can give me the correct code to do this it would be greatly appreciated. Thanks!

Lilluda 5
  • 1,111
  • 3
  • 18
  • 38
  • I had an issue in the past where I had ocamlbuild installed in two separate locations, so these two installations were checking different directories when searching for libraries. You could try: "/home/chris/.opam/bin/ocamlbuild -use-ocamlfind -pkgs cohttp.lwt file.native" (correct the path if it's not quite right) to make sure you're not seeing the same behavior. – Charles Marsh Jun 04 '13 at 23:15

2 Answers2

7

Cohttp has been updated so I've corrected your code to use the latest version:

open Lwt;;
open Cohttp;;
(* a simple function to access the content of the response *)
let content = function
| Some (_, body) -> Cohttp_lwt_body.string_of_body body
| None -> assert false


(* launch both requests in parallel *)
let t = Lwt_list.map_p Cohttp_lwt_unix.Client.get
(List.map Uri.of_string
    [ "http://google.com";
    "http://yahoo.com" ])

(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content

(* launch the event loop *)
let v = Lwt_main.run t2

You can build with

ocamlbuild -use-ocamlfind -pkgs cohttp.lwt file.native

A couple of comments:

1) You should use the -use-ocamlfind with ocamlbuild to use opam (or any other installed ocaml libs)

2) To use cohttp with lwt you should use the cohttp.lwt package. Adding lwt as well is not strictly necessary.

rgrinberg
  • 9,638
  • 7
  • 27
  • 44
  • this did not work. I received an error saying Ocamlfind could not find package cohttp.lwt, also when i use the "-use-ocamlfind" flag in my original instruction this did not work either. I did paste your code in to my file, though. – Lilluda 5 May 28 '13 at 23:36
  • should `ocamlfind query cohttp.lwt` return a path, otherwise you haven't installed cohttp properly. – rgrinberg May 29 '13 at 02:44
  • Ocamlfind query cohttp.lwt says the package is not installed. I uninstalled my cohttp package, then re-installed it with opam, and I am still receiving the same error. Is this possibly an OPAM issue? – Lilluda 5 May 29 '13 at 22:32
  • install lwt and then install cohttp. Otherwise yes I'm not sure what the problem could be, check which version of cohttp you have running. `opam info cohttp` – rgrinberg May 30 '13 at 03:20
  • I removed both packages, and reinstalled them in the order you suggested, and this did not work. I am running Ocaml 3.12.1 on Ubuntu 12.04, if it matters. – Lilluda 5 May 30 '13 at 20:59
  • I'm really stumped as to what the problem could be but you really should run 4.00.1. Just opam switch to it and then install the packages. I'm not sure it would work though. What does opam say for the version of cohttp you have running? – rgrinberg May 31 '13 at 00:15
  • Package cohttp is already installed (current version is 0.9.7) – Lilluda 5 May 31 '13 at 00:20
  • You should install the latest version (0.9.8) You might need to switch to the latest compiler for that however. I think that is the problem. – rgrinberg May 31 '13 at 00:24
  • So I've upgraded the compiler to 4.00.1, and ran opam install cohttp, and opam stated that the latest version had already been installed. – Lilluda 5 Jun 01 '13 at 18:02
  • Then check which version of cohttp you're running after doing `opam switch 4.00.1` `eval 'opam config -env'` (backquotes inside not single quotes) and `opam info cohttp` – rgrinberg Jun 01 '13 at 20:11
  • Ok, so I did everything you asked and it has installed cohttp 0.9.8 now! This is great, however when I run ocamlfind query cohttp.lwt it is still returning "package not found". Any ideas? – Lilluda 5 Jun 01 '13 at 20:33
  • Now I can run ocamlfind query cohttp.lwt, and it returns the same directory as what ocamlfind query cohttp does. Is this correct? Also, if I try running the command line you provided in your OP, it says the package is not found. – Lilluda 5 Jun 01 '13 at 20:51
  • Hmm this is my mistake, you should do `opam install ssl` and opam should rebuild lwt & cohttp for you. I forgot about this step since I was running cohttp from the source.. – rgrinberg Jun 01 '13 at 23:20
  • I remembered to install this. That was not the problem. – Lilluda 5 Jun 02 '13 at 01:31
  • Also, when i run eval `opam config env` it appears that nothing happens, is it silent? – Lilluda 5 Jun 02 '13 at 14:38
  • Thanks for all the pointers rgrinberg. I wish this had been in the opam manuals – Jens Jensen Jun 05 '13 at 15:02
  • I think that the problem is that `ocamlbuild` uses a fixed path to locate `ocamlfind`'s executable (something like `/usr/bin/ocamlfind`). I do not know how to change that in particular; a possible hack could be to overwrite the OCAMLFIND_CONF and OCAMLFIND_LDCONF environment variables. – nberth Dec 18 '13 at 09:14
1

I resolved this issue by uninstalling the version of ocaml-findlib that I had installed through my distro's package manager. For some reason, ocamlbuild was trying to use it instead of the version provided by opam, despite the latter being first on my $PATH.

The version of ocamlfind that had been installed via my distro's package manager could not find the local packages I had installed via opam.

According to http://brion.inria.fr/gallium/index.php/Using_ocamlfind_with_ocamlbuild, ocamlbuild has included support for ocamlfind via the -use-ocamlfind flag since 3.12 so you should be good with that regard. You can check via ocamlbuild --help | grep ocamlfind. If your version supports it, then you should be able to build your package as @rgrinberg described.

Dylon
  • 1,730
  • 15
  • 14
  • I have the same problem, except without root access I can't remove distro's `ocamlfind`. I wonder if I could "shadow" it somehow?.. – George Karpenkov Dec 10 '13 at 14:46