OCaml: Reference to undefined global `A` - ocaml

I have a three signatures A.mli,B.mli,and C.mli. Within them I have submodules Aa, Bb, and Cc respectively and also a signature for the implementations Ai, Bi, and Ci.
I have implemented them in a file called D.ml which looks like this
open A
open B
open C
module Ai : Aa = struct ... end
module Bi : Bb = struct ... end
module Ci : Cc = struct ... end
I compile them each in turn with the following commands:
ocamlfind ocamlc -linkpkg -thread -package core A.mli
ocamlfind ocamlc -linkpkg -thread -package core B.mli
ocamlfind ocamlc -linkpkg -thread -package core C.mli
ocamlfind ocamlc -linkpkg -thread -package core D.ml
which all succeed.
I then have a file E.ml where I try to use the modules Ai, Bi, Ci.
open A
open B
open C
module M = Ai(Ci)
(* do something else *)
However when I try to compile E.ml I get the
Error: Error while linking E.cmo:
Reference to undefined global `A`
I think I have to somehow include D.ml in the compilation step but I'm unsure how to.

Using ocamlc or ocamlopt compiler directly (even with the help of ocamlfind) is not an easy task, and should be left only for those, who write OCaml tools and very sure in what they're doing. So, in short, just use ocamlbuild, that is the default way of compiling things in OCaml world. Since, you're using Core library it would be also a good idea to use corebuild instead of ocamlbuild. corebuild is shipped with core, and is actually a small wrapper around ocamlbuild, that adds core dependencies, as well as some handy flags. So, with corebuild you can compile just with:
corebuild E.native
Without corebuild, you can compile with
ocamlbuild -pkg core E.native
ocamlbuild will do all the work for you, i.e., find all dependencies, sort them in correct order, invoke proper tools, etc... You just need this one, command.
If you're still interested in how to compile your example project properly, then you can ask ocamlbuild about this. Use -classic-display option, that will show you all intermediate commands,
ocamlbuild -use-ocamlfind -classic-display -pkg core E.byte
Will show you all steps. (You may need to ocamlbuild -clean, so that you can see all the steps from the start).

Related

How to link OCaml object file with Core module?

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.

OCaml: how to solve findlib warnings of multiple `cmi`s

Now I try to write a compiler using ocamlfind and some packages, especially ppx_sexp_conv with opam.
Somehow dependencies of ppx_sexp_conv require compiler-libs so the compilation option -I ~/.opam/VERSION/lib/compiler-libs is added when compiling files by ocamlfind ocamlc -package ppx_sexp_conv.
The problem is, compiler-libs includes very common name files like parsing.cmi, main.cmi, lexing.cmi.
This make conflicts of .cmi files and make many noisy warnings as follows:
$ ocamlfind ocamlc -package ppx_sexp_conv -c parser.mli
findlib: [WARNING] Interface main.cmi occurs in several directories: ., /home/nomaddo/.opam/4.02.3/lib/ocaml/compiler-libs
findlib: [WARNING] Interface lexer.cmi occurs in several directories: ., /home/nomaddo/.opam/4.02.3/lib/ocaml/compiler-libs
findlib: [WARNING] Interface topdirs.cmi occurs in several directories: /home/nomaddo/.opam/4.02.3/lib/ocaml/compiler-libs, /home/nomaddo/.opam/4.02.3/lib/ocaml
findlib: [WARNING] Interface parser.cmi occurs in several directories: ., /home/nomaddo/.opam/4.02.3/lib/ocaml/compiler-libs
```
Note that main.cmi, parsing.cmi, lexing.cmi and main.cmi exist in the same directory.
I believe that such file names are common and everybody want to use.
My question is, how to quiet such noisy warnings.
Thanks to them, it is hard to find more important warnings and errors immediately...
My environment: ocaml 4.02.3 with opam 1.2.2.
One way to suppress those warnings is to set findlib environment variable OCAMLFIND_IGNORE_DUPS_IN to /home/nomaddo/.opam/4.03.0/lib/ocaml/compiler-libs.
Here is an example with OCaml 4.03.0 and ppx_sexp_conv version 113.33.01+4.03.
parser.mli:
type t = int [##deriving sexp]
In shell, do the following
export OCAMLFIND_IGNORE_DUPS_IN=/home/nomaddo/.opam/4.03.0/lib/ocaml/compiler-libs
ocamlfind ocamlc -package ppx_sexp_conv -dsource -c parser.mli
You can see the .mli is preprocessed and no extra warning is emitted.
Reference:
findlib man page
Mantis 6754

Are ocamlfind package predicates implemented?

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.

(OCamlfind) build library with dependencies to Core

I'm trying to build a small library that has dependencies to Core and Core_extended. I followed the instructions under Where to place a shared utility module in OCaml? and installed the library in ocamlfind (declaring a dependency to Core_extended and Core in the META-file).
Using it in the top-level works fine now.
However, when I try to build a test-file using this library, it doesn't detect the dependencies to Core and Core_extend automatically. Using these compile instructions
ocamlfind ocamlopt -c -g -package my_lib -thread -o file.cmx file.ml
ocamlfind ocamlopt -g -linkpkg -package my_lib -thread file.cmx -o file.native
I get the following error:
Error: No implementations provided for the following modules:
Core referenced from /.../mylib.cmx
Core_extended referenced from /.../mylib.cmx
If I compile the file using the extra arguments "-package core_extended" then it compiles, however this is quite impractical.
Is there a way to make this dependency transparent to the compilation-process, such that "-package my_lib" automatically loads in Core and Core_extended?
EDIT:
This is the content of my META-file:
name="my_lib"
description="small library"
version="0.1"
depends="core,core_extended"
archive(byte)="my_lib.cmo"
archive(native)="my_lib.cmx"
EDIT2:
Renaming "depends" to "requires" in the META-file fixed this!
You should write requires instead of depends

How to use modules with js_of_ocaml?

I am currently working on a website project written in OCaml and compiled to javascript using js_of_ocaml. It works pretty well as long as I have only one source file using the command ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml.syntax -syntax camlp4o -linkpkg -o file.byte file.ml but I would like to include several modules in my project. How can I do that ?
The other modules are actually a lexer and a parser poduced by ocamllex and menhir. I have read a tutorial on how to use ocamllex and menhir with js_of_ocaml but it makes wrong assumptions on where js_of_ocaml is installed (I installed it with opam) and it uses ocamlbuild and I want to know how to do it by hand without using an automated tool such as ocamlbuild.
I found the solution by trying to understand the makefiles for the official examples.
Here is my Makefile :
OBJS=file1.cmo file2.cmo file3.cmo
NAME=projectname
OCAMLC=ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml.syntax -syntax camlp4o
$(NAME).byte: $(OBJS)
$(OCAMLC) -linkpkg -o $# $(OBJS)
$(NAME).js: $(NAME).byte
js_of_ocaml $<
%.cmo: %.ml
$(OCAMLC) -c $<
...
ocamlbuild keeps a log of the operations it performs. After an ocamlbuild call, look at _build/_log and you will see all the commands that it has invoked, with full arguments etc. That's probably the easiest way for you to figure how to do it by hand.
(Regarding +site-lib assumptions and OPAM, that's something you should report to the authors of the tutorial, they'll want to make sure that it also works for OPAM users.)
It's taken a bunch of experimentation, but I finally figured out how to have ocamlbuild pass the same flags to ocamlfind as ocsigen use in those makefiles. I'm also using js_of_ocaml installed with OPAM.
For my test case, I created a very small example with two files - main.ml and square.ml.
square.ml:
let square x = x * x
main.ml:
let () = (Js.Unsafe.coerce Dom_html.window)##square <- Js.wrap_callback Square.square
The command to build this successfully:
ocamlbuild -use-ocamlfind -pkgs js_of_ocaml,js_of_ocaml.syntax -syntax camlp4o main.byte
This produces identical JS output to my initial test case where the square function was in main.ml. The ocamlbuild log shows exactly what I expect (two calls to ocamldep, two to ocamlc -c, one to ocamlc -linkpkg).