Category Archives: Compiler

Compiling and Executing

Well I have been studying Ocaml for some days now but I have been using the Ocaml top-level to test my code until now. 

Therefore, I decided to grab a book and start understanding how to build a small project.


ByteCode vs NativeCode

For starters, there are two compilers, one generates portable code and the other one  specific architecture performance programs.

They byteCode compiler, from what I could understand, works like Java Virtual Machine. It is, in fact, a Virtual Machine, its name is Zinc (Zinc is not Caml). The byteCode compiler generates Zinc instructions. 

This way code can be run in any system where a byteCode compiler as been developed for, just like JVM.

The native compiler is optimized for the architecture of the system it was designed for.

ocamlc (ByteCode compiler)

ocamlopt (NativeCode compiler)



Compiling a single file is as simples as doing it with a C compiler:

ocamlc -o executalbe_file

Its analogous to the native compile. Its quite simple, nothing to add here.


Running Toplevel

If you are using, or just started working with, Ocaml, the first thing you notice is that you have a nice little thing called Toplevel execution. If you used MATLAB, like I did, the first thing you will think is: “This is just like MATLAB console”

Basically yes, it runs Ocaml code right on the fly, which is very useful, for debugging or testing.

But, I haven’t studied much about it in particular. I will try to do it later, because it has some interesting directives like #trace.



Now, the reason why I made this little summary, Modules. I need modules, I think everyone needs. 

So, I decided to go a bit back and try to understand how it works, from compiling a simple file to testing it.

I want to make a small project that simulates an Anti-Missil (check project here), and for that I want to use modules.

Modules in Ocaml, so far look like they are the same as in Java or C, you make them in different files (usually) and compile each one individually and compile the interfaces with the main file in the end. 

I will test a small example to see if I am able to do a simple module compile and execution:

I created a small program that defined a record and a method for this record, and another program that would create a value of type record A and call the method on it. Therefore depends on

Compiling the files according to dependency, first then, worked fine.

I guess makefiles will be useful in the future.

For now I will stop my experiments with modules and try to start solving my project: Anti Missile.




This post will be reviewed as soon as I can so it has the correct information. Right now it was written as a form of a work log.


Thank you,

João Bastos


Tagged , ,