7

我正在尝试制作这段代码:

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

但是,当我跑步时

Ocamlbuild file.native

我收到未绑定的模块错误。

这些模块是通过 opam 安装的,当我运行时

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

如何让 Ocamlbuild 找到这两个包?

我试过了

Ocamlbuild -pkgs cohttp,lwt file.native 

它没有用。它谈到了一个可能不正确的扩展名。我不认为这是问题所在。

如果有人能给我正确的代码来做到这一点,将不胜感激。谢谢!

4

2 回答 2

7

Cohttp 已更新,因此我已更正您的代码以使用最新版本:

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

你可以用

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

一些评论:

1)您应该使用-use-ocamlfindwithocamlbuild来使用 opam(或任何其他已安装的 ocaml 库)

2) 要将 cohttp 与 lwt 一起使用,您应该使用该cohttp.lwt软件包。添加lwt也不是绝对必要的。

于 2013-05-28T03:00:28.787 回答
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.

于 2013-09-27T07:40:49.683 回答