2013-05-28 61 views
7

安装的软件包,我试图让这段代码: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_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 

,并没有奏效。它说了一些关于可能延期的事情。我不认为这是问题。

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

+0

我在过去有一个问题,我在两个不同的位置安装了ocamlbuild,因此这两个安装程序在搜索库时检查不同的目录。你可以尝试:“/home/chris/.opam/bin/ocamlbuild -use-ocamlfind -pkgs cohttp.lwt file.native”(如果它是不完全正确更正路径),以确保您没有看到相同的行为。 –

回答

7

Cohttp已更新,我已经纠正了你的代码,使用最新版本:

1):

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 

一对夫妇的意见建设您应该使用-use-ocamlfindocamlbuild使用OPAM(或任何其他已安装ocaml的LIBS)

2)cohttp使用与LWT你应该使用cohttp.lwt包。加入lwt也不是绝对必要的。

+0

这没有奏效。我收到一个错误,说Ocamlfind无法找到软件包cohttp.lwt,当我在我的原始指令中使用“-use-ocamlfind”标志时,这也不起作用。不过,我的确将代码粘贴到了我的文件中。 –

+0

应该'ocamlfind查询cohttp。lwt'返回一个路径,否则你没有正确安装cohttp。 – rgrinberg

+0

Ocamlfind查询cohttp.lwt说包没有安装。我卸载了我的cohttp软件包,然后用opam重新安装了它,并且仍然收到相同的错误。这可能是一个OPAM问题? –

1

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

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

根据http://brion.inria.fr/gallium/index.php/Using_ocamlfind_with_ocamlbuild,ocamlbuild已包含支持ocamlfind通过-use-ocamlfind标志自3.12以来,所以你应该很好的这方面。您可以通过ocamlbuild --help | grep ocamlfind进行检查。如果你的版本支持它,那么你应该能够按照@rgrinberg所描述的来构建你的软件包。

+0

我有同样的问题,除了没有root访问权限,我无法删除发行版的'ocamlfind'。我想知道我能否以某种方式“遮蔽”它? –