"Error: unbound module" in OCaml - ocaml

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

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

Try to compile it like this ...
ocamlfind ocamlopt -o my_test \
-linkpkg \
-package lwt,cohttp,cohttp-lwt-unix \
-thread
my_test.ml

Related

How to link OCaml object file with Core module?

I have simple program, which uses function from Core.Std module, e.g.:
open Core.Std
let _ = List.last [1;2;3]
I can compile and link it by ocamlfind or ocamlbuild:
ocamlfind ocamlc -package core -linkpkg -thread app.ml
ocamlbuild -use-ocamlfind -pkgs core -tag thread app.native
But I don't know how to compile and link such file using "normal" OCaml compiler:
ocamlc -c -I ~/.opam/system/lib/core_kernel/ -I ~/.opam/system/lib/core/ app.ml
ocamlc ~/.opam/system/lib/core_kernel/core_kernel.cma ~/.opam/system/lib/core/core.cma app.cmo
Last of above commands gives the following error:
File "_none_", line 1:
Error: Error on dynamically loaded library: /home/maciej/.opam/system/lib/stublibs/dllcore_kernel_stubs.so: /home/maciej/.opam/system/lib/stublibs/dllcore_kernel_stubs.so: undefined symbol: caml_ba_alloc
What am I doing wrong?
You can use ocamlfind ocamlc -only-show … to see the full command constructed by ocamlfind . Your error points towards some missing transitive dependencies of Core and an old version of OCaml and Core.

ocamlopt and ocamlbuild give an Unbound module error despite ocamlfind seeing the required module

I'm attempting to build the xencat tool from this project. When I try to use ocamlopt to build it, I get
$ ocamlopt -o xencat xencat.ml
File "xencat.ml", line 1, characters 5-13:
Error: Unbound module Cmdliner
Following guidance here, I verified that cmdliner was installed and that it was visible to ocamlfind, and then tried again:
Here's what ocamlfind list shows:
$ ocamlfind list
bigarray (version: [distributed with Ocaml])
bytes (version: [distributed with OCaml 4.02 or above])
...
cmdliner (version: 0.9.8)
...
No luck:
$ ocamlfind ocamlopt -o xencat xencat.ml
File "xencat.ml", line 1, characters 5-13:
Error: Unbound module Cmdliner
Following this page, I tried ocamlbuild:
$ocamlbuild -use-ocamlfind xencat.byte
+ ocamlfind ocamlc -c -o xencat.cmo xencat.ml
File "xencat.ml", line 1, characters 5-13:
Error: Unbound module Cmdliner
Command exited with code 2.
Clearly I'm missing something, but being new to Ocaml, I don't know what else to try.
ocamlbuild -use-ocamlfind -package cmdliner xencat.byte
?
To build this project use the following command at the root of the project:
./configure
make build
You will find a xencat.native file in the root folder.
You can also use opam to build it, as opam will handle all dependencies to you. Just go to the root of the project and say:
opam pin add vchan .
and answer yes to whatever opam asks to do.

Ocamlbuild doesn't take in account the included folders

I am trying to use ocamlbuild to build my project. It needs to take into account an external library (also compiled with ocamlbuild) that contains in the folder myfolder/ the following files (myfolder is a symlink to the good library that I create in the file myocamlbuild.ml):
$ ls _build/myfolder/
PhTools.cmi PhTools.ml PhTools.mli
PhTools.cmo PhTools.ml.depends PhTools.mli.depends
To compile my library I run the following command:
ocamlbuild -use-ocamlfind -plugin-tags "package(eliom.ocamlbuild),package(containers),package(fileutils)" -Is "myfolder" -mods "myfolder/PhTools.cmo" _server/./TestHelloServices.cmo
When I run it I have the error:
[...]
ocamlfind ocamlc -I '' -I _server -I _type -i -thread -package eliom.server -package eliom.syntax.predef -package eliom.syntax.type -syntax camlp4o _type/TestHelloServices.ml > _type/TestHelloServices.inferred.mli
+ ocamlfind ocamlc -I '' -I _server -I _type -i -thread -package eliom.server -package eliom.syntax.predef -package eliom.syntax.type -syntax camlp4o _type/TestHelloServices.ml > _type/TestHelloServices.inferred.mli
File "TestHelloServices.eliom", line 6, characters 10-17:
Error: Unbound module PhTools
Command exited with code 2.
As you can see the option -I myproject hasn't been given to the command ocamlfind ocamlc -i .... You can note that if I add by hand ocamlfind ocamlc -i -I 'myproject' this step works. I tried lot's of differents things to solve my problem : using -cflags '-I,myproject' -lflags '-I,myproject', putting in _tags the line:
myproject: include
but I always have this error.
I don't know if it's a relevant information but I use a slightly modified version of the ocsigen myocamlbuild.ml file:
let client_dir = "_client"
let server_dir = "_server"
let type_dir = "_type"
module M = Ocamlbuild_eliom.Make(struct
let client_dir = client_dir
let server_dir = server_dir
let type_dir = type_dir
end)
open Ocamlbuild_plugin;;
let () =
dispatch begin function
| Before_options ->
M.dispatcher Before_options;
(* Link root project *)
(try
Unix.symlink "../../../../../myfolder/" "_build/myfolder"
with Unix.Unix_error (Unix.EEXIST, _, _)-> ());
| hook -> M.dispatcher hook
end;
Thank you in advance,
TobiasBora.
I guess ocamlbuild will not follow "external" (out of project tree) links.
Maybe hardlink will work, but better yet install the library as the normal ocamlfind package.

Basic Ocaml: How do I compile this?

Just beginning with ocaml and am struggling with the various compilers and tooling. E.g. ocamlopt, ocamlc, ocamlbuild, corebuild and so on. So, how do I compile the following?
open Core.Std
module Regex = Re2.Regex
let ls pattern =
let pat = Regex.create_exn pattern in
let matcher = Regex.matches pat in
Sys.ls_dir "."
|> List.filter ~f:matcher
|> List.iter ~f:(fun s -> print_string s; print_newline ())
let () =
match In_channel.input_line stdin with
| None -> print_string "No Input"
| Some pat -> ls pat
In utop I can just #require "re2" and go from there.
Without the inclusion of the regular expressions module I would just use corebuild ls.native, assuming that the above code is placed into ls.ml.
[edit]
Have so far tried
ocamlbuild -use-ocamlfind -package core -package re2
which spit out
ocamlfind ocamldep -package core -package re2 -modules ls.ml > ls.ml.depends
ocamlfind ocamlc -c -package core -package re2 -o ls.cmo ls.ml
+ ocamlfind ocamlc -c -package core -package re2 -o ls.cmo ls.ml
ocamlfind: Error from package `threads': Missing -thread or -vmthread switch
Command exited with code 2.
So after some googling I was led to this blog I tried
ocamlbuild -tag thread -use-ocamlfind -package core -package re2
which spits out over 6000 lines of what looks like make output before failing with:
collect2: error: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking
Command exited with code 2.
so I'm not sure what to try next.
I am using Ubuntu 14.04 on a 64bit machine. I ditched the apt-get versions of ocaml:
sudo apt-get remove --purge ocaml ocaml-base-nox ocaml-compiler-libs \
ocaml-interp ocaml-native-compilers \
ocaml-nox campl4 ocaml-base ocaml-docs opam
Then I installed opam from source according to the instructions here.
Next I installed core, utop, and re2 with opam install core utop re2
And finally I ran ocamlbuild -use-ocamlfind -package re2 -package core -tag thread ls.native
which built the desired executable.
I would suggest you to use oasis tool. It is not as hard, as one may think. But it removes all library burden from you.
Lets create a simple _oasis file for your project:
OASISFormat: 0.4
Name: ls
Version: 0.1
Synopsis: Testing oasis
Authors: Fizz_ed
License: MIT
Plugins: META (0.4), DevFiles (0.4)
BuildTools: ocamlbuild, camlp4o
BuildDepends: core, camlp4, threads,
sexplib.syntax,
bin_prot.syntax,
comparelib.syntax,
herelib,
herelib.syntax
Executable "ls"
Path: .
MainIs: ls.ml
CompiledObject: best
BuildDepends: re2
After you've created this file, run oasis setup command (if oasis is not installed, then install it using opam, or your package manager).
After that you will have a common configure script and makefiles. So all you need is to type
./configure
make
and your file will be compiled.
Or you can just use ocamlbuild directly, it will work too, since oasis created all the necessary files
ocamlbuild ls.native
A few explanations about the file. The preamle contains everything you need to build your core-style applications.
Executable section describes your executable file, and also adds a dependency to re2 library.
Update: fixed some issues.

OCaml bindings for Z3: Success in OCaml interpreter, but fail in OCaml compiler

I got some problems between OCaml interpreter and OCaml compiler. Can anyone help me? Thank you so much!
I have just compiled successfully the OCaml bindings for Z3 following by the instructions at https://github.com/polazarus/z3-installer. They use an old Z3 version: 4.1
First, I change the file Makefile.ocaml:
# Findlib package installation obtion, for instance -destdir /usr/lib/ocaml
OCAMLFIND_INSTALL_FLAGS = -destdir /home/maidinh/.opam/4.01.0/lib/
Then, I compile it:
sudo apt-get install camlidl
sudo make
sudo make install
I don't know why it fails when run 'make' without 'sudo' permission:
the terminal log when I run 'sudo make' : http://pastebin.com/rhdAMJKf
the log when I run only 'make' : http://pastebin.com/qvhysRum, it says that "ld: cannot find -lcamlidl" (Can anyone explain this problem to me?)
Finally, I test the OCaml bindings for Z3 by running the OCaml interpreter 4.01.0:
./ocaml
#use "topfind";;
#require "z3";;
open Z3;;
Z3.mk_context;;
- : (string * string) list -> Z3.context = <fun>
Successfully!
However, my program failed to run using OCaml compiler. This is my program:
let _ = print_endline "Start" in
let _ = Z3.mk_context [] in
()
Then, I compile and run:
ocamlfind ocamlc -linkpkg -package z3 -c main.ml -o main.cmo
ocamlfind ocamlc -linkpkg -package z3 -o main main.cmo
./main
Start
Error: internal error
Can anyone explain the error to me? Thank you so much!
In a nutshell, Z3 4.1's ML binding is buggy. I revert it back to 4.0 on the repository of z3-installer. To compile:
ocamlfind remove z3
opam install camlidl
git clone https://github.com/polazarus/z3-installer.git # fresh clean install
cd z3-installer
make
sudo make lib-install
make ocaml-install
Note that the API is slightly different, your example should read:
let _ = print_endline "Start" in
let _ = Z3.mk_context_x [||] in
()
To install and compile the new Z3 with ML bindings, you should have a look at Drup's Z3 overlay. He explains how to compile Z3.