繁体   English   中英

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

[英]“Error: unbound module” in 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 "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 实际包含/要求到我的应用程序中?

更新

还有:

$ 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 for the impatient

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错误,我可以建议确保您的环境配置正确。 Real World OCaml 书有一个 wiki 页面: https : //github.com/realworldocaml/book/wiki/Installation-Instructions

尝试像这样编译它......

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