下面是一个使用库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找到和建屋在哪里?
update2
$ ocamlfind ocamlc -package lwt -c my_test1.ml
File "my_test1.ml", line 2, characters 5-11:
Error: Unbound module Cohttp发布于 2018-01-02 17:31:33
根据您的需要,您有几种选择。
1)如果您想为二进制文件创建一个完整的项目,我建议查看jbuilder。下面是一个非常好的指南,它逐步解释了环境/项目配置:OCaml治疗不耐烦者。
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 )这本书有一个维基页面:https://github.com/realworldocaml/book/wiki/Installation-Instructions。
发布于 2020-08-24 19:22:40
试着像这样编译..。
ocamlfind ocamlopt -o my_test \
-linkpkg \
-package lwt,cohttp,cohttp-lwt-unix \
-thread
my_test.mlhttps://stackoverflow.com/questions/48058993
复制相似问题