I am aware of similar questions on this site but none of them have helped to solve my problem.
I am very new at OCaml and am using the following tutorial about using Camlp4 https://github.com/ocaml/camlp4/wiki/OCaml_code_generation_tutorial
However I get an error on the first line:
open Camlp4.PreCast
saying "Unbound module Camlp4"
There is a camlp4.exe file where I have downloaded OCaml so I assumed it was installed. I have tried this both on Windows 8 and Xubuntu
This is the input I am passing to the command line to compile the file:
ocamlc -o test.exe test.ml
where test.ml is the file that contains the line with the error
You should know that camlp4 is deprecated, you should use ppx instead.
If you need to compile an old programs using camlp4, they should have instructions on how to build. Anyway it's very rare to use ocamlc directly, you should try ocamlbuild or at least use ocamlfind.
Related
I'm having trouble linking a very simple OCaml program:
open Core
Format.printf "hello world %s\n" "foobar";;
Format.printf "argv= %s\n" (Sys.get_argv()).(0) ;;
which I compile with
ocamlfind ocamlc -thread -package core visitor.ml
The compile step always generates the error:
Error: Required module `Core__Core_sys' is unavailable
I've pinned version 4.0.9, and I can see the file:
$ ocamlfind query core
/home/ubuntu/.opam/4.09.0/lib/core
and $ ls -la /home/ubuntu/.opam/4.09.0/lib/core shows
-rw-r--r-- 1 ubuntu ubuntu 17891 Dec 3 20:14 core__Core_sys.cmi
-rw-r--r-- 1 ubuntu ubuntu 93777 Dec 3 20:14 core__Core_sys.cmt
-rw-r--r-- 1 ubuntu ubuntu 75659 Dec 3 20:14 core__Core_sys.cmti
-rw-r--r-- 1 ubuntu ubuntu 16958 Dec 3 20:14 core__Core_sys.cmx
I've tried everything I can think of, with no luck. BTW, I notice that the documentation https://ocaml.org/api/Sys.html makes no mention at all of get_argv but if I try just plain Sys.argv I get a warning:
# Sys.argv ;;
Alert deprecated: Core.Sys.argv
[since 2019-08] Use [Sys.get_argv] instead, which has the correct behavior when [caml_sys_modify_argv] is called.
So I conclude that the core OCaml documentation published at ocaml.org is more than two years out of date! How can one obtain up-to-date documentation, ideally documentation that describes these kinds of newbie errors?
A few points. First, it looks like you are on a fairly old version of OCaml. Unless you need to stay on 4.09 for some reason, I highly recommend upgrading to the latest version, 4.13.1. Instructions for installing are here: https://ocaml.org/learn/tutorials/up_and_running.html
In case you have any trouble with that, try upgrading to the latest version of opam (the OCaml package manager) and doing opam update to download the most up-to-date package index.
Second, it looks like you are trying to use Jane Street's Core library, which is a third-party package which is intended as a standard library replacement. As such, it has its own version of the OCaml standard library's Sys module, i.e. Core.Sys. Regarding the alert that you are getting, the Core.Sys.argv value is actually deprecated by Jane Street Core: https://ocaml.janestreet.com/ocaml-core/latest/doc/core/Core__/Core_sys/index.html#val-argv
A single result from get_argv (). This value is indefinitely deprecated. It is kept for compatibility...
This leads us to the final issue, when you try to compile it is unable to find the core package. There are a couple of choices here. First, one option is that the core package and standard library replacement are actually optional; you may not actually need them. If you're an OCaml beginner you could try sticking with the standard library only (so no open Core, no trying to compile with the core package).
Another option, if you decide to keep using the core package, is to the dune build system instead of ocamlfind. Dune is a powerful, modern OCaml build system that handles almost all aspects of package linking during the build, so you don't need to worry about issuing individual compile commands.
Here's what a dune file would look like:
(executable
(name visitor)
(libraries core))
And the visitor.ml file would be in the same directory:
let () =
Printf.printf "hello world %s\n" "foobar";
Printf.printf "argv= %s\n" Sys.argv.(0)
Then you would run:
dune exec ./visitor.exe
The .exe is a dune convention, executables across operating systems are given this extension.
Finally, in source code you never actually need ;;. More about that here: https://discuss.ocaml.org/t/terminate-a-line-with-or-or-in-or-nothing/8941/21?u=yawaramin
Note on documentation being out of date: it would help if you could point us to where you got the instructions that led you to this point of confusion. There is a lot of effort to clean up install instructions and modernize documentation, but unfortunately there are a lot of outdated getting started guides out there. The 'Up and Running' link I provided at the top of this answer is the best resource.
You need to link the package by adding the -linkpkg flag:
ocamlfind ocamlc -thread -package core -linkpkg visitor.ml
I'm trying to use the "Batteries" module, but... it doesnt work!
Here is an example:
open Batteries;;
print_endline (dump [5;4;2]);;
I'm compiling with: opam exec ocamlc main.ml the error message is:
File "main.ml", line 1, characters 5-14:
1 | open Batteries;;
^^^^^^^^^
Error: Unbound module Batteries
and the Batteries module seems installed:
$ opam install Batteries
[NOTE] Package batteries is already installed (current version is 3.0.0).
What am I missing?
Just installing a library is not enough, you have to tell the compiler that you want to link it (the compiler itself doesn't know anything about opam). There are many compilation and configuration tools available for OCaml, here's just one of the ways to build a binary that uses batteries
ocamlbuild -pkg batteries main.native
Preface: I am new to OCaml, OPAM, and OASIS.
tldr question: How do I properly set up a package with opam that is not already available in the repository (I can't just do opam install X)? More details follow:
I am trying to include ocaml-glpk in an OCaml project. I installed ocaml-glpk just by running make and make install as stated in the README, and the given example compiles and runs correctly. However, I am using OASIS to generate the build system of my project, and I am not sure how to set it up. I have the same example (renamed to glpkExample.ml in a src folder) and the following in my _oasis file:
Executable "glpkExample"
Path: src
MainIs: glpkExample.ml
CompiledObject: best
BuildDepends:
glpk
After running oasis setup -setup-update dynamic, I run make and get the following error:
ocaml setup.ml -build
Finished, 0 targets (0 cached) in 00:00:00.
+ /home/dimitrios/.opam/system/bin/ocamlfind ocamlopt -g -linkpkg -package glpk src/glpkExample.cmx -o src/glpkExample.native
File "_none_", line 1:
Error: Cannot find file /home/dimitrios/.opam/system/lib/glpk/glpk.cmxa
Command exited with code 2.
Compilation unsuccessful after building 4 targets (3 cached) in 00:00:00.
E: Failure("Command ''/usr/bin/ocamlbuild' src/glpkExample.native -tag debug' terminated with error code 10")
make: *** [build] Error 1
It seems the glpk library is missing a cmxa file needed to compile a native executable. I am not sure how to fix this. To compile glpkExample.ml correctly, my Makefile includes /home/dimitrios/.opam/system/lib/glpk and also uses the OCamlMakefile, which is extremely long and convoluted. Any help on setting this up with OASIS or how to get ocaml-glpk to work nicely with OASIS would be greatly appreciated.
Thanks!
This website is not appropriate for bug reports. You should really report it here.
The temporary solution is to use CompiledObject: byte to compile in bytecode.
If you're using opam then it is best to install application with it, not manually. Try to clean up your system and remove whatever you installed, and then do:
$ eval `opam config env`
$ opam install ocaml-glpk
Afterwards, if glpk is packaged in opam correctly, it should work with your setup, i.e., just with oasis's BuildDepends field and nothing more.
In my project I have a file that uses Core.Std stuff, so I have run
opam install core
and added
open Core.Std
in my file.
When I run
ocamlbuild myprogram.native
it says:
Error: Unbound module Core
pointing to line with the open statement above.
So, I try this:
ocamlbuild -use-ocamlfind -pkgs core.std myprogram.native
and get the following message:
ocamlfind: Package `core.std' not found
So I thought that maybe I needed to run opam install core.std as well, but apparently there is no such thing according to opam. I also tried "open Core.Std;;" in the ocaml repl, but that did not work either. Any ideas?
You can either use corebuild which is usually shipped with this library or, you can try this:
ocamlbuild -use-ocamlfind -pkg core
P.S. use ocamlfind list command to view the list of available packages.
P.P.S. In addition to corebuild they usually ship coretop, a script that allows you to run core-enabled top-level. It uses utop underneath the hood, so make sure that you have installed it with opam install utop (if you're using opam), before your experiments.
Remove .std from your ocamlbuild cmd?
There seems to be conflicting information about batteries installation. I have tried several suggestions, but none have worked for me.
I first tried
ocamlfind batteries/ocaml
but that gave this error:
ocamlfind: Cannot find command: /username/godi/lib/ocaml/pkg-lib/batteries/ocaml
I then tried copying the ocamlinit file from the batteries directory to .ocamlinit in my home directory. This gave this error:
Cannot find file topfind.
File ".ocamlinit", line 38, characters 0-20:
Error: Unbound module Toploop
I am using ocaml 4.00.1.
Note: I apologize if this question is redundant with this one ocaml batteries compiling : Unbound module Toploop but the answer given was not explicit enough for me to actually try.
Like #rgrinberg said, try to install batteries with opam. For that, first download the quick installer:
$ wget http://www.ocamlpro.com/pub/opam_installer.sh
Then execute this script:
$ sh ./opam_installer.sh /usr/local/bin
It will install the latest "stable" opam (you can of course change the path /usr/local/bin) and the latest version of the OCaml compiler.
After that, you just need to run:
$ opam install batteries
and it should be ok.
You can also check ocaml.org install section (by package manager) or opam website.