Addem
Addem

Reputation: 3929

Compiling and Running Ocaml Script from the Terminal

I have files called assert.ml, assert.mli, test.ml and test.mli stored in a folder. assert is I guess you'd call it a library file--it's something I downloaded but didn't write myself. test.ml is a file containing the script

;; open Assert 
;; print_endline "test"

In the terminal I navigate to the containing folder and run

$ ocamlc -c assert.mli test.mli
$ ocaml assert.ml test.ml

Nothing happens as a result. However, if I remove the ;; open Assert line from the script and run

$ ocaml test.ml

then it prints.

As a note, I've had some people tell me not to write the open command as ;; open Assert but the advice seems entirely stylistic. I have to write it this way for the class I'm taking.

If anyone can explain to me how I ought to be compiling and running differently I'd appreciate it. I tried following some other guides' advice in using ocamlopt instead but when I ran it, no executable file was produced as a result.

Upvotes: 3

Views: 9346

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

The ocaml command is the REPL for OCaml. The command line looks like this:

ocaml [ object-files ] [ script-file ]

An object file is a compiled (bytecode) module, which is produced by the ocamlc command. These files end with .cmo. A script file is a file of OCaml source code, which ends with .ml.

Note that only one script file is allowed.

The command you say you're using has two script files and no object files. So, it's not surprising that it doesn't work. In my experiments, what ocaml does is run just the first of the two script files. I believe this explains what you see. One of your files produces output, and it will be run if you give it first. The other produces no output, so there is no output when that file is given first.

What you should probably be doing is producing a .cmo file for the Assert module.

That would look like this:

$ ocamlc -c assert.mli assert.ml test.mli

Then you should run ocaml with one object file and one script file, like this:

$ ocaml assert.cmo test.ml

Upvotes: 6

Related Questions