"错误:未绑定模块"在OCaml

时间:2018-01-02 10:10:48

标签: 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在哪里?

UPDATE2

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

2 个答案:

答案 0 :(得分:8)

根据您的需要,您有多种选择。

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书籍有一个维基页面:https://github.com/realworldocaml/book/wiki/Installation-Instructions

答案 1 :(得分:0)

尝试像这样编译它...

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