“错误:未绑定的模块";在OCaml中 [英] "Error: unbound module" in OCaml
问题描述
这是使用库Cohttp的简单示例:
Here's a simple example of using the library 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
Error: Unbound module 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在哪里?
Where are ocamlfind and ocamlbuild located?
更新2
$ ocamlfind ocamlc -package lwt -c my_test1.ml
File "my_test1.ml", line 2, characters 5-11:
Error: Unbound module Cohttp
推荐答案
您可以根据需要选择几种方法.
You have several options depending on your needs.
1)如果您要为二进制文件创建一个完整的项目,建议您查看 jbuilder .这是一个非常好的指南,它逐步解释了环境/项目配置: OCaml对于急躁的人.
1) If you want to create a full project for your binary I recommend looking at jbuilder. Here is a very nice guide that explains the environment/project configuration step-by-step: OCaml for the impatient.
2)另一个选择是像您尝试的那样直接编译二进制文件:
2) Another option is to compile the binary directly as you were trying to do:
ocamlbuild -pkg lwt -pkg cohttp-lwt-unix my_test1.native
请注意,您需要有一个名为my_test1.ml
的文件才能生成所请求的my_test1.native
.
Note that you need to have a file named my_test1.ml
to generate the requested my_test1.native
.
3)最后,对于快速脚本,我发现可以方便地要求OCaml解释器将依赖项直接加载到源文件中非常方便.只需将以下内容添加到文件的开头即可:
3) And finally for quick scripts I find it handy to be able to ask the OCaml interpreter to load the dependencies directly in the source file. Just add the following to the beginning of your file:
#use "topfind";;
#require "lwt";;
#require "cohttp-lwt-unix";;
然后运行ocaml my_test1.ml
.
希望这会有所帮助! :)
Hope this helps! :)
还要查看您遇到的command not found
错误,我建议您确保正确配置您的环境.真实世界的OCaml书籍为此提供了一个Wiki页面: https://github.com/realworldocaml /book/wiki/安装说明
Also looking at the command not found
errors you are getting I can suggest to make sure your environment is correctly configured. The Real World OCaml book has a wiki page for that: https://github.com/realworldocaml/book/wiki/Installation-Instructions
这篇关于“错误:未绑定的模块";在OCaml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!