简体   繁体   English

OCaml 中的“错误:未绑定模块”

[英]“Error: unbound module” in OCaml

Here's a simple example of using the library Cohttp:这是使用库 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)

I'm trying to compile it我正在尝试编译它

 ocaml my_test1.ml

Error:错误:

Error: Unbound module Lwt错误:未绑定模块 Lwt

How to actually include/require the module Lwt into my app?如何将模块 Lwt 实际包含/要求到我的应用程序中?

update更新

Also:还有:

$ ocamlbuild
bash: ocamlbuild: command not found

But:但是:

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

And并且

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

And并且

$ ocamlfind
bash: ocamlfind: command not found

Where are ocamlfind and ocamlbuild located? ocamlfind 和 ocamlbuild 位于何处?

update2更新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) If you want to create a full project for your binary I recommend looking at jbuilder . 1)如果您想为您的二进制文件创建一个完整的项目,我建议您查看jbuilder Here is a very nice guide that explains the environment/project configuration step-by-step: OCaml for the impatient .这是一个非常好的指南,它逐步解释了环境/项目配置: OCaml for the impatient

2) Another option is to compile the binary directly as you were trying to do: 2)另一种选择是直接编译二进制文件,就像你试图做的那样:

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

Note that you need to have a file named my_test1.ml to generate the requested my_test1.native .请注意,您需要有一个名为my_test1.ml的文件来生成请求的my_test1.native

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. 3) 最后,对于快速脚本,我发现能够让 OCaml 解释器直接在源文件中加载依赖项很方便。 Just add the following to the beginning of your file:只需将以下内容添加到文件的开头:

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

And then run ocaml my_test1.ml .然后运行ocaml my_test1.ml


Hope this helps!希望这有帮助! :) :)

Also looking at the command not found errors you are getting I can suggest to make sure your environment is correctly configured.同时查看您收到的command not found错误,我可以建议确保您的环境配置正确。 The Real World OCaml book has a wiki page for that: https://github.com/realworldocaml/book/wiki/Installation-Instructions Real World OCaml 书有一个 wiki 页面: https : //github.com/realworldocaml/book/wiki/Installation-Instructions

Try to compile it like this ...尝试像这样编译它......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM