English 中文(简体)
ocamlc, module compilation
原标题:
  • 时间:2009-12-04 01:42:24
  •  标签:
  • ocaml

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




相关问题
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 ...

How can I simplify this ocaml pattern-matching code?

I m writing a simple little ocaml program that reads an algebraic statement in from a file, parses it into an AST using ocamllex/ocamlyacc, reduces it, and then prints it. The part where I m reducing ...

How can I create a type with multiple parameters in OCaml?

I m trying to create a type that has multiple type parameters. I know how to make a type with one parameter: type a foo = a * int But I need to have two parameters, so that I can parameterize the ...

Hashtable indexed on several fields

I m currently programming an OCaml module defining a type corresponding to a CPU register. The interface of this module is the following : (* * Defines a type which represents a R3000 register. *) ...

Extending an existing type in OCaml

I ve been doing some OCaml programming lately to learn the language and to get more acquainted with functional programming. Recently, I ve started to think that I d like to be able to extend an ...

Ocaml Syntax Error

What s wrong with this code? I can t figure it out: let parent (rules : grammar) (symbol1 : string) (symbol2 : string) : (SymbolSet.t) = try SymbolSet.singleton (getParent [symbol1; symbol2] ...

热门标签