Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ocamlbuild and Packages installed via Opam

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!

like image 760
Lilluda 5 Avatar asked May 28 '13 02:05

Lilluda 5


People also ask

Where are opam packages installed?

Your libraries are installed to the directory returned by opam var lib , which is by default ~/. opam/<switch>/lib .

What is opam Linux?

opam is a source-based package manager for OCaml. It supports multiple simultaneous compiler installations, flexible package constraints, and a Git-friendly development workflow. Managing your OCaml installation can be as simple as: opam list -a # List the available packages.

What is an opam switch?

opam switchThis command enables the user to have several installations on disk, each with their own prefix, set of installed packages, compiler version, etc. Use cases include having to work or test with different OCaml versions, keeping separate development environments for specific projects, etc.

How do I change opam switch?

opam switch set sets the default switch globally, but it is also possible to select a switch in a given shell session, using the environment. For that, use eval $(opam env --switch=SWITCH --set-switch).


2 Answers

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.

like image 193
rgrinberg Avatar answered Sep 28 '22 04:09

rgrinberg


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.

like image 45
Dylon Avatar answered Sep 28 '22 02:09

Dylon