1

So I am writing a multiple-file program in OCaml and my directory structure looks like this:

src
 |- module1.ml
 |- section1
     |- module2.ml
     |- module3.ml

Where module1 opens module2 and module3, and module2 opens module3.

I compile the program with the following:

ocamlopt -o bin/myprog src/module1.ml src/section1/module2.ml src/section1/module3.ml

It throws in error in module2.ml saying that Module3 is unbound.

Does anyone know why this is happening?

1 Answer 1

1

First, note that the order in which .cmx and .ml arguments are presented on the command line is relevant. This means that module1 which appears to be using both module2 and module3 should be in last position and not first as you're doing here.

You will also need to add the -I directory option to add the given directory to the list of directories searched for.

This should do the job:

ocamlopt -o bin/myprog -I src/section1 src/section1/module3.ml src/section1/module2.ml src/module1.ml
Sign up to request clarification or add additional context in comments.

4 Comments

Oops. I meant module2 says that module3 is unbound. My bad.
ok than it should be sufficient to put the module in the right order when calling ocamlopt, isn't it?
Apparently not. Maybe it'll work if I compile them to bytecode first, then use ocamlopt and compile them into native code?
Ok so you need to add the -I dir option to have it work as i show in my edited answer. I've also removed the cyclic dependancy thing as it is not useful anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.