21

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)

I'm trying to compile it

 ocaml my_test1.ml

Error:

Error: Unbound module Lwt

How to actually include/require the module Lwt into my app?

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?

update2

$ ocamlfind ocamlc -package lwt -c my_test1.ml 
 File "my_test1.ml", line 2, characters 5-11:
 Error: Unbound module Cohttp
Loku
  • 363
  • 1
  • 2
  • 7
  • Possible duplicate of [Difference between module and package Ocaml](https://stackoverflow.com/questions/16681939/difference-between-module-and-package-ocaml) – coredump Jan 02 '18 at 11:15
  • @coredump it's not – Loku Jan 02 '18 at 11:19
  • 2
    did you run 'eval $(opam config env)' before running your commands ? (it should solve 'command not found' issues). Regarding lwt and cohttp, you may miss some package on the ocamlbuild commands (-pkg lwt and so on) – Pierre G. Jan 02 '18 at 12:00
  • @PierreG., no. Where is it said that I need to run it? – Loku Jan 02 '18 at 12:28
  • @PierreG., see my update – Loku Jan 02 '18 at 12:32
  • 1st question : generally this is what opam tells after running 'opam switch ' , so not your case. 2nd question : from https://github.com/mirage/ocaml-cohttp : 'ocamlbuild -pkg cohttp-lwt-unix client_example.native'. so I guess 'ocamlfind ocamlc -package cohttp-lwt-unix -c my_test1.ml' might work (... I do not have ocaml environment under my hand currently) – Pierre G. Jan 02 '18 at 15:07
  • @PierreG., thx. – Loku Jan 02 '18 at 15:24

2 Answers2

11

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. Here is a very nice guide that explains the environment/project configuration step-by-step: OCaml for the impatient.

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

Note that you need to have a file named my_test1.ml to generate the requested 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. 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.


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. The Real World OCaml book has a wiki page for that: https://github.com/realworldocaml/book/wiki/Installation-Instructions

Rizo
  • 3,003
  • 5
  • 34
  • 49
  • and cohttp_lwt_unix is available thx to 'opam install cohttp-lwt-unix' (underscore '_' of the package name becomes a dash '-'). – Pierre G. Jan 02 '18 at 20:20
  • i'd run "opam init" before I posted my question – Loku Jan 03 '18 at 01:02
  • `opam init` will update your shell profile to include the correct env settings. But the current shell will still have the old environment. So you need to run "eval `opam config env`" to load the environment in the current shell (with backquote around "opam config env"). – Rizo Jan 03 '18 at 14:24
  • 2) Unknown directive `require'. – Loku Jan 04 '18 at 15:11
  • `$ opam install topfind` ====> `[ERROR] No package named topfind found.` – Loku Jan 04 '18 at 15:12
  • I'm pretty sure your environment is not correctly set up. Do you have an an `~/.ocamlinit` file with the following content? `(* Added by OPAM. *) let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with Not_found -> () ;;` – Rizo Jan 08 '18 at 17:40
  • Run `opam init` and try adding "eval `opam config env`" to your shell's init file (`~/bashrc` if your'e using bash). – Rizo Jan 08 '18 at 17:43
  • building with dune fixed it for me – David 天宇 Wong Sep 11 '22 at 08:12
0

Try to compile it like this ...

ocamlfind ocamlopt -o my_test \ 
   -linkpkg \ 
   -package lwt,cohttp,cohttp-lwt-unix \
   -thread 
    my_test.ml
phage
  • 584
  • 3
  • 17