why I can't use Async.Std? - ocaml

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.

Related

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).

OCaml's utop: importing external modules

I'm interested in introducing Angstrom, a parsing combinator library, to a project of mine.
I installed Angstrom to my system with opam as follows:
$> opam install angstrom
I also successfully compiled the project with reference to the module in question using ocamlfind:
$> ocamlbuild -use-ocamlfind -pkgs 'angstrom' project.byte
Surprisingly, I'm having trouble importing Angstrom to the utop repl. Commands like #open Angstrom indicate a value bound to the module name. I have searched for documentation of module imports from the repl, but I haven't found the result that I'm looking for.
What's the best way to reference an external module from utop?
I found an answer to my question. From utop:
utop # #require "angstrom";;
utop # open Angstrom;;
My apologies for perhaps posting prematurely, but hopefully someone will find this answer helpful in the future.

ocaml-glpk (glpk bindings) and OASIS

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.

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

Telling ocamlbuild to use Core

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?