Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top