Unbound module core while trying to compile ocaml file - ocaml

i'm having a problem while trying to compile an ocaml file with ocamlc -o hello hello.ml it gives me this error
Error: Unbound module Core
which is weird because when i use utop and import the core std with open Core.Std;; it does work and import it, any ideas of how to solve this problem ?
Thanks in advance

open Core.Std does not really import core, it just puts its values in scope so that you can refer to Core.Std.x as just x.
To import it you need to pass it to require the package somehow in your compiler. The easiest way is to use ocamlfind:
ocamlfind ocamlc -package core -linkpkg -o hello hello.ml
The corresponding way to do that in utop is by passing -require core on the command line or #require "core" in the REPL.

Related

ocamlfind complains no implementation provided for modules installed through opam

I was messing around with the OCaml FFI to try to figure out how it goes about inferring the width of a C enum (which I think is implementation-defined for C) and am trying to insert a type of the wrong width to see what blows up at runtime. That's the motivation, but the actual issue I ran into is much more mundane.
I have a simple OCaml file that uses the C FFI to call a trivial function in example.c that converts an enum to an int.
open Printf;;
let (#->) = Ctypes.(#->);;
let returning = Ctypes.returning;;
let foreign = Foreign.foreign;;
(* deliberately use the wrong scalar type for argument *)
let wrong_int64_of_color =
foreign "int_of_color" (Ctypes.int64_t #-> returning Ctypes.int64_t);;
let main () =
printf "%Ld\n" (wrong_int64_of_color (Int64.of_int 100));;
let () = main ();;
I configured opam and installed Ctypes and Ctypes.Foreign
% opam config env | sed -e 's/=.*/=/'
CAML_LD_LIBRARY_PATH=
OPAMUTF8MSGS=
MANPATH=
PERL5LIB=
OCAML_TOPLEVEL_PATH=
PATH=
% opam list | grep ctypes
ctypes 0.6.2 Combinators for binding to C libraries withou
ctypes-foreign 0.4.0 Virtual package for enabling the ctypes.forei
The two usual incantations I use for compiling a simple .ml script have both failed me and I'm out of ideas. ocamlfind and corebuild (which I think is a wrapper on top of ocamlbuild)
ocamlfind can't seem to find ctypes and foreign. However, it doesn't complain that about not being able to locate the packages so I'm guessing ctypes and ctypes.foreign are the correct names for these packages in the weird findlib namespace.
% ocamlfind ocamlopt -package findlib,ctypes,ctypes.foreign -thread call_example.ml
File "_none_", line 1:
Warning 58: no cmx file was found in path for module Foreign, and its interface was not compiled with -opaque
File "call_example.ml", line 1:
Error: No implementations provided for the following modules:
Ctypes referenced from call_example.cmx
Foreign referenced from call_example.cmx
Why can't ocamlfind locate these modules? I have no problem loading them into the toplevel.
─( 22:30:42 )─< command 0
utop # #require "ctypes";;
─( 22:30:42 )─< command 1
utop # open Ctypes;;
─( 22:30:55 )─< command 2
utop # #require "ctypes.foreign";;
─( 22:31:00 )─< command 3
utop # open Ctypes;;
It seems to me that you forget to add -linkpkg ocamlfind option to instruct the compiler actually link the libraries from the packages to build the executable.

Unbound module Camlp4

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.

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?

OCaml CSV module - ERROR: Unbound Module Csv

I have installed the OCaml CSV module in C:\OCaml\lib. When I run the following code in the top level open Csv;;
I receive the error message Error: Unbound module Csv
I am not sure why the module is not loading.
Thanks
There are two steps involved. First you need to make sure that CSV loaded into your toplevel, then you can open it. I don't use windows, but under OS X it looks like this:
$ ocaml
OCaml version 4.00.1
# #load "unix.cma";;
# open Unix;;
# stat;;
- : string -> Unix.stats = <fun>
#
I just found the solution. I did not compile the csv.ml and csv.mli files.
If anybody else has this problem here are the steps to fix the problem:
1) In the command line go to the OCaml\lib directory where your .ml and .mli files are located
2) Run the commands ocamlc -c modulename.mli and ocamlc -c modulename.ml
in the toplevel you can now run open modulename;;

Ocaml topfind lwt

I am trying to use lwt module with Ocaml
%cat .ocamlinit
#use "topfind";;
#require "lwt.simple-top";;
#require "lwt.syntax";;
#require "findlib";;
#require "num.core";;
#load "nums.cma";;
#load "unix.cma";;
#camlp4o;;
open Lwt;;
open Lwt_io;;
When I try to use any Lwt API using this command to build
"ocamlbuild -use-ocamlfind myFile.native"
I receive this error:
"Error: Unbound module Lwt"
When I type in terminal %ocaml it loads successfully and i can use Lwt.API's
What is going on? any suggestion?
ocamlbuild doesn't read .ocamlinit, it is purely for toplevel (i.e. ocaml).
In order to compile lwt-dependent project you will need to specify dependencies, e.g. :
ocamlbuild -use-ocamlfind -package lwt myFile.native
See also How to compile ocaml to native code