簡體   English   中英

通過Opam安裝Ocamlbuild和Packages

[英]Ocamlbuild and Packages installed via 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 

它不起作用。 它說了一些可能的擴展。 我不認為這是問題所在。

如果有人能給我正確的代碼來做到這一點,將不勝感激。 謝謝!

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-ocamlfindocamlbuild來使用opam(或任何其他已安裝的ocaml庫)

2)要使用cohttp和lwt,你應該使用cohttp.lwt包。 添加lwt也不是絕對必要的。

我通過卸載我通過我的發行版的軟件包管理器安裝的ocaml-findlib版本解決了這個問題。 出於某種原因, ocamlbuild試圖使用它而不是opam提供的版本,盡管后者首先出現在我的$PATH

通過我的發行版軟件包管理器安裝的ocamlfind版本找不到我通過opam安裝的本地軟件包。

http://brion.inria.fr/gallium/index.php/Using_ocamlfind_with_ocamlbuildocamlbuild包括了支持ocamlfind通過-use-ocamlfind標志,因為3.12,所以你要善於用這方面。 你可以通過ocamlbuild --help | grep ocamlfind檢查ocamlbuild --help | grep ocamlfind ocamlbuild --help | grep ocamlfind 如果你的版本支持它,那么你應該能夠像@rgrinberg所描述的那樣構建你的包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM