Home > OCaml, Windows > Making Win32 GUI programs in OCaml, continued #1. Native compilation

Making Win32 GUI programs in OCaml, continued #1. Native compilation

I keep playing with OCaml, and it’s very fun.

First funny thing is that OCaml could be used to compile C/C++ files. Of course, it doesn’t contain C compiler inside, it invokes some external compiler. Which compiler to invoke depends on version of OCaml, for Windows it supports Microsoft Visual C++ and MinGW. Thos e compilers should be possible to locate via PATH, because OCaml simply invokes “gcc” or “cl”. Why such feature is implemented – I don’t know, because compiler could always be invoked directly. Probably OCaml adds it’s own include and libary directories for compiler and linker.

OCaml can produce two types of code: byte-code and native code. Byte code available for much wider range of platforms then native code. Byte code files are very similar to object files produced by C compiler: they have extensions .cmo, they could be organized in libraries (they have extensions .cma). Byte code files could be executed by OCaml virtual machine called “ocamlrun”. Compiler is also able to produce stand-alone executables which contain both virtual machine runtime and byte code for your program. Such stand-alone program is usual exe file and could be executed on any Windows machine. In this case linker is invoked (from MinGW or Microsoft Visual C++). Stand-alone programs also able to contain staticaly-linked object code, written in C/C++ or assembly.

Native compiler produces native code. It does that in two-step approach: first, assembly source generated, then external assembler is invoked for compilation. OCaml version for Microsoft Visual C++ invokes Microsoft Assembler (ml.exe), so it is required for native compilation. I didn’t checked that, but I suppose that Ocaml version for MinGW invokes GNU assembler. After compiling all modules are linked using external linker. Native executables are also stand-alone, and they are usually faster then byte-code.

I’ve discovered that by playing with ocaml-win32. By default only byte-code version of library is compiled. To compile native version of library, I’ve invoked “nmake win32.cmxa”, and got “command not found: ml.exe”. So I’ve downloaded Microsoft Assembler and then everything were build succesfully.

I like OCaml more and more. It’s approach to compilation seems very good to me. And ability to interface C makes it very powerful.

Categories: OCaml, Windows
  1. jmol
    April 15, 2010 at 2:10 pm

    Hi kmatveev!
    This is the third article from you taht i read. I like your hacker definition and your effort to te(a)ch us. I like options to microsoft world but included in a microsoft environment is hard to find a language that can do similar things to his programming languages and i haven’t enough time (and perhaps entusiasm) to learn as you.
    At the end, figuring if OCAML deserves the time to try it and if can do a decent gui, connect to databases and not to require a hard time to code.
    Keep you great job and fun!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: