OCaml 中的"Error: unbound module"



下面是使用库Cohttp的简单示例:

open Lwt
open Cohttp
open Cohttp_lwt_unix
let body =
  Client.get (Uri.of_string "http://www.reddit.com/") >>= fun (resp, body) ->
  let code = resp |> Response.status |> Code.code_of_status in
  Printf.printf "Response code: %dn" code;
  Printf.printf "Headers: %sn" (resp |> Response.headers |> Header.to_string);
  body |> Cohttp_lwt.Body.to_string >|= fun body ->
  Printf.printf "Body of length: %dn" (String.length body);
  body
let () =
  let body = Lwt_main.run body in
  print_endline ("Received bodyn" ^ body)

我正在尝试编译它

 ocaml my_test1.ml

错误:

错误:未绑定模块 Lwt

如何在我的应用程序中实际包含/要求模块 Lwt?

更新

也:

$ ocamlbuild
bash: ocamlbuild: command not found

但:

$ opam install ocamlbuild
[NOTE] Package ocamlbuild is already installed (current version is
       0.12.0).

$ opam install ocamlfind
[NOTE] Package ocamlfind is already installed (current version is
       1.7.3-1).

$ ocamlfind
bash: ocamlfind: command not found

ocamlfind 和ocamlbuild 的地址在哪里?

更新2

$ ocamlfind ocamlc -package lwt -c my_test1.ml 
 File "my_test1.ml", line 2, characters 5-11:
 Error: Unbound module Cohttp
根据您的

需要,您有多种选择。

1(如果你想为你的二进制文件创建一个完整的项目,我建议你看看jbuilder。这里有一个非常好的指南,逐步解释了环境/项目配置:OCaml为不耐烦的人。

2(另一种选择是直接编译二进制文件,就像您尝试的那样:

ocamlbuild -pkg lwt -pkg cohttp-lwt-unix my_test1.native

请注意,您需要有一个名为 my_test1.ml 的文件才能生成请求的my_test1.native

3(最后,对于快速脚本,我发现能够要求OCaml解释器直接在源文件中加载依赖项很方便。只需将以下内容添加到文件的开头:

#use "topfind";;
#require "lwt";;
#require "cohttp-lwt-unix";;

然后运行ocaml my_test1.ml.


希望这有帮助! :)

还可以查看您遇到的command not found错误,我可以建议确保您的环境已正确配置。现实世界OCaml书有一个维基页面:https://github.com/realworldocaml/book/wiki/Installation-Instructions

尝试像这样编译它...

ocamlfind ocamlopt -o my_test  
   -linkpkg  
   -package lwt,cohttp,cohttp-lwt-unix 
   -thread 
    my_test.ml

最新更新