ocamlc, module compilation
I wrote an app in ocaml. It consist of several modules:
- Util (util.ml)
- Work1 (work1.ml) -- open Util
- Work2 (work2.ml) -- open Util, too
- Main (main.ml) -- open all of them.
开发者_运维知识库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!
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...
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.
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
精彩评论