Ocamlbuild and Packages installed via Opam
Asked Answered
P

2

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!

Puncture answered 28/5, 2013 at 2:38 Comment(1)
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.Nuncio
M
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.

Mendoza answered 28/5, 2013 at 3:0 Comment(17)
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.Puncture
should ocamlfind query cohttp.lwt return a path, otherwise you haven't installed cohttp properly.Mendoza
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?Puncture
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 cohttpMendoza
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.Puncture
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?Mendoza
Package cohttp is already installed (current version is 0.9.7)Puncture
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.Mendoza
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.Puncture
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 cohttpMendoza
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?Puncture
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.Puncture
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..Mendoza
I remembered to install this. That was not the problem.Puncture
Also, when i run eval opam config env it appears that nothing happens, is it silent?Puncture
Thanks for all the pointers rgrinberg. I wish this had been in the opam manualsBenefit
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.Hexachord
C
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.

Cockeyed answered 27/9, 2013 at 7:40 Comment(1)
I have the same problem, except without root access I can't remove distro's ocamlfind. I wonder if I could "shadow" it somehow?..Kandicekandinsky

© 2022 - 2024 — McMap. All rights reserved.