&引用;错误:未绑定模块";在OCaml中

&引用;错误:未绑定模块";在OCaml中,ocaml,Ocaml,下面是一个使用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 "Re

下面是一个使用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: %d\n" code;
  Printf.printf "Headers: %s\n" (resp |> Response.headers |> Header.to_string);
  body |> Cohttp_lwt.Body.to_string >|= fun body ->
  Printf.printf "Body of length: %d\n" (String.length body);
  body

let () =
  let body = Lwt_main.run body in
  print_endline ("Received body\n" ^ body)
我正在试着编译它

 ocaml my_test1.ml
错误:

错误:未绑定模块Lwt

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

更新

此外:

但是:

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) 如果你想为你的二进制文件创建一个完整的项目,我建议你看看。下面是一个非常好的指南,它一步一步地解释了环境/项目配置:

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


希望这有帮助!:)


另外,通过查看您收到的
命令notfound
错误,我可以建议确保您的环境配置正确。现实世界中的OCaml书籍有一个wiki页面:

试着像这样编译它

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

可能重复@coredump it's not在运行命令之前是否运行了“eval$(opam config env)”?(应解决“未找到命令”问题)。关于lwt和cohttp,您可能会错过ocamlbuild命令(-pkg lwt等)@PierreG.,否。它说我需要在哪里运行它?@PierreG.,请参阅我的更新,cohttp_lwt_unix可用于“opam安装cohttp lwt unix”(包名的下划线“u”变成破折号“-”),我会运行“opam init”在我发布我的问题之前,
opam init
将更新您的shell配置文件以包含正确的env设置。但当前的shell仍将保留旧环境。因此,您需要运行“eval
opam config env
”以在当前shell中加载环境(在“opam config env”周围加上反引号)。2)未知指令“require”。
$opam install topfind
====>
[错误]未找到名为topfind的包。
$ ocamlfind ocamlc -package lwt -c my_test1.ml 
 File "my_test1.ml", line 2, characters 5-11:
 Error: Unbound module Cohttp
ocamlbuild -pkg lwt -pkg cohttp-lwt-unix my_test1.native
#use "topfind";;
#require "lwt";;
#require "cohttp-lwt-unix";;
ocamlfind ocamlopt -o my_test \ 
   -linkpkg \ 
   -package lwt,cohttp,cohttp-lwt-unix \
   -thread 
    my_test.ml