Telling ocamlbuild to use Core - ocaml

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?

Related

Issue in opening a module in OCaml

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

ocaml command line cannot find “topfind”

I've installed opam, run opam init, run opam switch 4.06.0 which created a 4.06.0 directory inside ~/.opam, run "eval opam confing env" which exports $OCAML_TOPLEVEL_PATH as ~/.opam/4.06.0/lib/toplevel amongst other things, when launching ocaml I get the dreaded:
$ ocaml
OCaml version 4.06.0
Cannot find file topfind.
Unknown directive `camlp4o'.
#
I've looked at this and this neither of which address my issue and I'm at my wits' end (first time setting up OCaml). This is my ~/.ocamlinit:
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind"
#camlp4o
#thread
#require "core.top"
#require "core.syntax"
EDIT: Looks like I hadn't installed core, installing core resolved that but now amongst the slew of import diagnostics I get:
Exception:
Invalid_argument
"The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel".
And then a bit further down:
Raised at file "pervasives.ml", line 33, characters 25-45
Called from file "toplevel/toploop.ml", line 468, characters 4-128
Called from file "toplevel/topdirs.ml", line 144, characters 10-51
Camlp4 Parsing version 4.06.0
You should run
eval `opam config env`
Note the backticks. They are usually located to the left of the key 1 on most modern keyboards. The command should not output anything, if you see any output it means that you're running it incorrectly. You have to run this command to activate the opam installation every time you start a new shell (unless you've put this command in your shell initialization scripts, like .bashrc)
If the problem persists, then make sure, that you have installed the ocamlfind package,
opam install ocamlfind
What seemed to work for me:
make sure core is installed (opam install core)
make sure camlp4 is installed (opam install camlp4)
Insert Topfind.don't_load ["compiler-libs.toplevel"];; in-between #use "topfind";; and #require "core.top";;, as per this. It is an issue that doesn't appear to be fixed in the latest version of core (0.9.2).

why I can't use Async.Std?

I have install async mod by opam:
when input this in shell:
opam install async
shell returns:
[NOTE] Package async is already installed (current version is 113.33.03)
When I use utop,first #require "async";;
then I can open Async.Std
but when I write a program a.ml with one line:
open Async.Std
When I build the program:
corebuild a.native
It tells me it can't find async:
Error:Unbound module Async
I don't know why. Can someone help me? Thanks.
The corebuild program, that is just a script wrapping the ocamlbuild tool, needs to know which libraries are you going to use. The corebuild just adds the core library, but not async or anything else. So you need to specify which library you're using explicitly, e.g.,
corebuild -pkg async a.native
To put it simply, -pkg corresponds to the #require directive.

oasis picks up wrong ocamlbuild

I try to compile an Ocaml project with an Ocaml version provided by Opam.
My ocamlbuild, ocamlfind, and oasis seem OK :
/Users/fred/.opam/4.02.1/bin/ocamlbuild
dhcp-182-73:compil fred$ which ocamlfind
/Users/fred/.opam/4.02.1/bin/ocamlfind
dhcp-182-73:compil fred$ which oasis
/Users/fred/.opam/4.02.1/bin/oasis
But when I try to compile, it seems that a wrong version of ocamlbuild is called, and even the version of ocamlfind is right, I think that it explains why ocamlfind can't find the sexplib library.
$ make
ocaml setup.ml -build
Finished, 0 targets (0 cached) in 00:00:00.
+ /Users/fred/.opam/system/bin/ocamlfind ocamldep -package threads -package sexplib.syntax -package core -package comparelib.syntax -modules src/tricot/tricot.mli > src/tricot/tricot.mli.depends
ocamlfind: Package `sexplib.syntax' not found
Command exited with code 2.
Compilation unsuccessful after building 1 target (0 cached) in 00:00:00.
E: Failure("Command ''/usr/local/bin/ocamlbuild' src/tricot/tricot.cma src/tricot/tricot.cmxa src/tricot/tricot.a src/tricot/tricot.cmxs src/compil/compil.cma src/compil/compil.cmxa src/compil/compil.a src/compil/compil.cmxs src/main.native -use-ocamlfind -tag debug' terminated with error code 10")
make: *** [build] Error 1
I've tried to tell oasis which version of ocambuild to use without success, does someone knows how it could be done ?
Thanks
Try deleting setup.data and running make again. Oasis caches the paths the first time you try to build, and doesn't update them automatically afterwards. Perhaps you tried to build it, then used opam switch, then tried to build it again?
You must be missing a
eval $(opam config env)
See the documentation of opam switch.
Note that normally opam's install procedure should have made so that this gets invoked automatically on new shells. You may want to run opam init again it will prompt you to agree to make changes to your .profile so that everything is in order when you start new shells.
EDIT: In fact it looks like you changed to the system switch between the two invocations you show us. So it seems that in the system switch sexp is not installed. In anycase whenever you opam switch in a shell always invoke the command I mentioned above so that the right paths are setup.
In addition to Thomas's answer, you can discard the configuration by
ocaml setup.ml -distclean

ocaml batteries installation

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.