Ocamlbuild和通过Opam安装的软件包

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 

但是,它没有起作用。它显示了可能是不正确的扩展名。我认为这不是问题所在。

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


我曾经遇到过一个问题,就是我在两个不同的位置安装了ocamlbuild,因此这两个安装程序在搜索库时会检查不同的目录。你可以尝试使用"/home/chris/.opam/bin/ocamlbuild -use-ocamlfind -pkgs cohttp.lwt file.native"(如果路径不正确,请更正)来确保你没有看到相同的行为。 - Charles Marsh
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)你应该在使用ocamlbuild时使用-use-ocamlfind来使用opam(或任何其他安装的ocaml库)。

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


这个没有生效。我收到了一个错误,说Ocamlfind找不到包cohttp.lwt,而且当我在原始指令中使用“-use-ocamlfind”标志时,这也没有起作用。不过,我确实将你的代码粘贴到了我的文件中。 - Lilluda 5
包cohttp已经安装(当前版本为0.9.7) - Lilluda 5
好的,我按照您的要求做了一切,现在已经安装了cohttp 0.9.8!这很棒,但是当我运行ocamlfind query cohttp.lwt时,它仍然返回“找不到包”。有什么想法吗? - Lilluda 5
我记得安装了这个。那不是问题所在。 - Lilluda 5
我认为问题在于 ocamlbuild 使用固定路径来定位 ocamlfind 的可执行文件(类似于 /usr/bin/ocamlfind)。我不知道如何特别更改它;一个可能的解决方法是覆盖 OCAMLFIND_CONF 和 OCAMLFIND_LDCONF 环境变量。 - nberth
显示剩余12条评论

1

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

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

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


我有同样的问题,但是没有root访问权限,我无法删除发行版的ocamlfind。我想知道是否可以以某种方式“遮蔽”它?.. - George Karpenkov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接