2013-05-28 60 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'。我想知道我能否以某種方式「遮蔽」它? –