Rijhan Djappur
Rijhan Djappur

Reputation: 21

ocamlc, module compilation

I wrote an app in ocaml. It consist of several modules:

When i compile its, using ocamlc, compilation failed on module Work2, and i get error message about unbound value from Util. Separate compilation doesn't work, too. What i do wrong?

ocamlc -o app.out -vmthread -pp camlp4o.opt unix.cma threads.cma camlp4of.cma util.ml work1.ml work2.ml main.ml

Thanks!

Upvotes: 2

Views: 1075

Answers (3)

ygrek
ygrek

Reputation: 6697

Use ocamlbuild, it figures out dependencies by magic, builds in separate directory, easily integrates with ocamlfind (since 3.12) and is generally awesome.

Create _tags file with :

true: thread, package(unix)
<*.ml>: camlp4o

And build with

ocamlbuild -use-ocamlfind main.byte

Upvotes: 0

0xFF
0xFF

Reputation: 4178

If you have the modules like the following :

module Util
    ...
end;;

module Work2
     open Util
     ...
end;;

module Main
    open Util;;
    open Work2;;
    ...
end;;

Module Work1
    open Work2;;
    ...
end;;

then the order must be in the way that when each module calls open it find the opened module already compiler, in the example abover the order would be

Util -> Work2 -> Work1 -> Main

Notice that OCaml doesn't support cyclic redundency for modules, means you can't have

module Work1
       open Work2;;
end;;

module Work2
       open Work1;;
end;;

if your app is a little complicated with a lot of modules, you can use Ocamldep http://caml.inria.fr/pub/docs/manual-ocaml/manual027.html and it will figure out the graph dependency for you.

Upvotes: 0

Pascal Cuoq
Pascal Cuoq

Reputation: 80355

The order of files on the commandline is significant in OCaml. You must put the files in dependency order. This is probably the problem you are having. Try changing the order of the files until it works...

Upvotes: 2

Related Questions