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.
Related
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.
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
I have a package named sundialsml, in which I want to load a slightly different .cm(x)a file depending on whether a subpackage named no_sens is loaded. Promisingly enough, the findlib 1.6.2 reference manual describes a "package predicate" feature:
[...] there are package predicates for every package that is finally
selected. [They] have the form "pkg_" plus the name of the package
(fully qualified).
So I wrote up this META file, where archive branches on the package predicate for the subpackage:
version = "2.6.2"
description = "OCaml interface to Sundials"
requires = "bigarray"
archive(byte) = "sundials.cma"
archive(byte,pkg_sundialsml.no_sens) = "sundials_no_sens.cma"
archive(native) = "sundials.cmxa"
archive(native,pkg_sundialsml.no_sens) = "sundials_no_sens.cmxa"
package "no_sens" (
version = "2.6.2"
description = "Sundials/ML without sensitivity analysis (CVODE, IDA, KINSOL)"
requires = "sundialsml"
)
But findlib loads sundials.cma regardless of whether the subpackage no_sens is loaded, e.g.:
# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
- : unit = ()
# #require "sundialsml.no_sens";;
/home/jun/.opam/4.01.0/lib/ocaml/unix.cma: loaded
/home/jun/.opam/4.01.0/lib/ocaml/bigarray.cma: loaded
/home/jun/.opam/4.01.0/lib/sundialsml: added to search path
/home/jun/.opam/4.01.0/lib/sundialsml/sundials.cma: loaded
Same happens if I try branching on a top-level package, like ao. In fact, far as I can tell, predicates of the form pkg_foo are never defined (unless we say #predicates "pkg_foo";; of course).
Am I using package predicates incorrectly? Or are they not really implemented? If so, is there any other way to select different archives based on the presence/absence of subpackages?
Please note the point here is to compute the archive from the set of subpackages the user selected. So "why don't you use #predicates" is not the solution I'm looking for.
It looks like that the pkg_ predicates are not implemented for the #require directive at all. Of course, I may be wrong, since I inferred this by just greping the code and experimenting. In fact, it is only implemented in the frontend, so it is also not available if someone is using the library interface (so it will not work out of box for ocamlbuild). Also, the pkg_ predicate is set only for selected packages, not for installed one. Where selected means, that the package is in the set of dependencies.
Here is an example. We define package ttt with the following META:
archive(byte,pkg_ttt.foo) = "foo.cma"
archive(byte,pkg_ttt.bar) = "bar.cma"
package "foo" (
requires = "ttt"
)
package "bar" (
requires = "ttt"
)
Now we can verify, that it works:
$ ocamlfind c -only-show -linkpkg -package "ttt.bar" main.ml
ocamlc.opt -I opam/lib/ttt opam/lib/ttt/bar.cma main.ml
Note: I've used opam instead of a real path to my ocaml installation to shorten the output for readability.
$ ocamlfind c -only-show -linkpkg -package "ttt.foo" main.ml
ocamlc.opt -I opam/lib/ttt opam/lib/ttt/foo.cma main.ml
So, everything works, when we use the frontend. But if we will try from the toplevel:
# #require "ttt.foo";;
opam/lib/ttt: added to search path
Then nothing is loaded at all.
We can also try to use ocamlbuild:
$ ocamlbuild -classic-display -package ttt.foo main.byte
opam/bin/ocamldep.opt -modules main.ml > main.ml.depends
opam/bin/ocamlc.opt -c -I opam/lib/ttt -o main.cmo main.ml
opam/bin/ocamlc.opt -I opam/lib/ttt main.cmo -o main.byte
So nothing is linked, it doesn't work. But if you will use -use-ocamlfind option, it will work, since this option prescribes ocamlbuild to use ocamlfind fronted.
$ ocamlbuild -use-ocamlfind -classic-display -package ttt.foo main.byte
ocamlfind ocamldep -package ttt.foo -modules main.ml > main.ml.depends
ocamlfind ocamlc -c -package ttt.foo -o main.cmo main.ml
ocamlfind ocamlc -linkpkg -package ttt.foo main.cmo -o main.byte
So, to conclude, the idea was nice, and can work theoretically, but it is better not to use it, since the implementation is not complete.
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.
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.