Now that the OCaml distribution is available on your favorite operating system, we can create your first program in OCaml: the Hello World!
We have different ways to launch an OCaml program.
You can execute your code interactively with the toplevel. With the OCaml toplevel, you can write and execute OCaml code, as a UNIX shell. Afterwards, the toplevel checks the type of your code immediately. So, you can quickly and easily test some parts of code without compilation and execution.
You can launch the toplevel with the ocaml
command. Then, you can write an OCaml sentence ended by ;;
which is evaluated immediately. The toplevel displays the type and the value of your expression just after:
# "Hello Worlds!";;
- : string = "Hello Worlds!"
It is also possible to launch the toplevel on your file. You can see this explanation about that.
To facilitate your input in the toplevel, you can use a tool like ledit
or rlwrap
which provides some features (like input history):
$ ledit ocaml
$ rlwrap ocaml
We have two different compilers, one which compiles to bytecode and the other which compiles to native code. The first is the same as the bytecode of the Java's virtual machine. So, the bytecode is less efficient but more portable.
We have some extensions files used by the OCaml compilers:
extension | definition |
---|---|
.ml | The source code (as .c in C) |
.mli | The interface (as .h in C) |
.cmo | Source code compiled by ocamlc in bytecode |
.cmi | Interface code compiled by ocamlc |
.cmx and .o | Source code compiled by ocamlopt in native code |
.cma | Library (bucket of some *.cmo ) in bytecode |
.cmxa and .a | Library in native code |
.cmxs | Library in native code (to load dynamicaly) |
The bytecode compiler is ocamlc
.
You have different common options:
-c
: to compile a source file without the linkage process (to produce an executable). So, the command ocaml -c foo.ml
produces a .cmo
file. Unlike C in which the header file does not need to be compiled, it's necessary in OCaml to compile the .mli
file: ocaml -c foo.mli
.You need to compile the interface first. When you compile the source file afterwards, OCaml tries to check that the implementation matches the interface.
The .mli
file is not a mandatory. If you compile a .ml
file without a .mli
file, OCaml will produce a .cmi
file automatically.
-o
: to compile some .cmo
files to an executable. For example: ocamlc -o program foo.cmo bar.cmo
. These files need to be arranged by the dependencies for which the first file has no dependence.
-I
: to indicate an other directory where the compiler can find the necessary files for the compilation (like the interface or source code). It's the same than the -I
from a C compiler.
We have many other options. You can see the manual for more information.
So, you can write the hello.ml
now, and compile this file with ocamlc -o hello hello.ml
to produce a bytecode program:
let () = print_endline "Hello World!"
The let () = ...
is the first entry of your program (like the main
in C). After, we use the function print_endline
(provided by the standard library) with the argument "Hello World!"
to print Hello Worlds
with a newline in the standard output.
After the compilation, you have the .cmo
file and the .cmi
file automatically produced by the compiler and your program hello
. You can open your program, and in the top of this file, you can see:
#!/usr/local/bin/ocamlrun
That means your program need the ocamlrun
program (provided by the distribution) to execute the bytecode (like the JVM).
We have an another compiler that produces native code. The compiler is: ocamlopt
. However, the resultant executable can't work on most other architectures.
ocamlopt
uses the same options as ocamlc
so you can execute ocamlopt -o hello hello.ml
. After, you can see a .cmx
and a .o
file.
Finally, from your bytecode/native code program, you can execute:
$ ./hello
Hello World!
$