通过Opam安装的Ocamlbuild和包

通过Opam安装的Ocamlbuild和包,ocaml,ocamlbuild,opam,Ocaml,Ocamlbuild,Opam,我正在尝试制作这段代码: 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

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

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 
但它没有起作用。它说了一个可能不直接的扩展。但我认为这不是问题所在


如果有人能给我正确的代码来做这件事,将不胜感激。谢谢

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-ocamlfind
ocamlbuild
一起使用opam(或任何其他已安装的ocaml库)


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

我通过卸载通过发行版的软件包管理器安装的ocaml findlib版本解决了这个问题。出于某种原因,
ocamlbuild
试图使用它,而不是
opam
提供的版本,尽管后者是我的
$PATH
上的第一个版本

通过发行版的软件包管理器安装的
ocamlfind
版本找不到我通过
opam
安装的本地软件包


根据,
ocamlfuild
从3.12开始就通过
-use-ocamlfind
标志提供了对
ocamlfind
的支持,因此在这方面应该做得很好。您可以通过
ocamlbuild--help | grep ocamlfind
进行检查。如果您的版本支持它,那么您应该能够按照@rgrinberg所述构建包。

这不起作用。我收到一个错误,说Ocamlfind找不到包cohttp.lwt,当我在原始指令中使用“-use Ocamlfind”标志时,这也不起作用。不过,我确实将您的代码粘贴到了我的文件中。应该
ocamlfind query cohttp.lwt
返回一个路径,否则您没有正确安装cohttp。ocamlfind query cohttp.lwt说包没有安装。我卸载了cohttp包,然后用opam重新安装了它,我仍然收到相同的错误。这可能是OPAM问题吗?安装lwt,然后安装cohttp。否则是的,我不确定问题可能是什么,请检查您正在运行哪个版本的cohttp
opam info cohttp
我删除了这两个软件包,并按照您建议的顺序重新安装了它们,但这不起作用。如果有必要的话,我正在Ubuntu 12.04上运行Ocaml 3.12.1。过去我有一个问题,我在两个不同的位置安装了ocamlbuild,所以这两个安装在搜索库时检查不同的目录。您可以尝试:“/home/chris/.opam/bin/ocamlbuild-使用ocamlfind-pkgs cohttp.lwt file.native”(如果路径不太正确,请更正路径),以确保没有看到相同的行为。我也有同样的问题,除了没有根访问权限,我无法删除发行版的
ocamlfind
。我想知道我是否能“阴影”它?。。