0

I have two separate OCaml files as follows

a.ml

let hello str = "hello from" ^ str

and b.ml

A.hello "Module B"

to compile and run, I did the following one after the other

ocamlc -c a.ml
ocamlc -c b.ml
ocamlc -o a.cmo b.cmo

the first two command runs without an error.But when I execute the last command I get the following error

> File "_none_", line 1: Error: Error while linking b.cmo: Reference to
> undefined global `A'

How do I fix this?

nlucaroni
  • 47,556
  • 6
  • 64
  • 86
DesirePRG
  • 6,122
  • 15
  • 69
  • 114
  • See [accepted answer to this question][1], there is a complete example. [1]: http://stackoverflow.com/questions/22028151/separate-compilation-of-ocaml-modules – Str. Apr 30 '14 at 22:05
  • Possible duplicate of [Compiling and running in Ocaml](http://stackoverflow.com/questions/34028789/compiling-and-running-in-ocaml) – Nathan Tuggy Dec 02 '15 at 01:16

1 Answers1

3

ocamlc -o myprogram.byte a.cmo b.cmo maybe ? man ocamlc may be useful.

Daniel Bünzli
  • 5,119
  • 20
  • 21
  • how do i run the resulting byte file? – DesirePRG Apr 30 '14 at 16:16
  • It's an executable, just as normal. If you didn't notice, `a.cmo` is being set as the out file, not as an additional file to link against. – nlucaroni Apr 30 '14 at 16:34
  • I do not get an output in my terminal when i run ./myprogram.byte – DesirePRG Apr 30 '14 at 16:36
  • 1
    Why would you? You aren't printing anything. – nlucaroni Apr 30 '14 at 17:06
  • yeah okay. I noticed something unusual though. when i run "ocamlc -o myprogram a.cmo b.cmo" it works.The error appears only when i don't specify the output file name.the man page says the default will be named a.out though – DesirePRG May 01 '14 at 04:32
  • 1
    If you specify `-o a.cmo` it's going to write to the file `a.cmo`, again `man ocamlc`, `-o` has a mandatory argument. If you want the default exec name don't specify `-o`. – Daniel Bünzli May 01 '14 at 09:55