Sunday, October 7, 2012

Compiling .NET code in Console mode and Form mode with Mono C# Compiler

Compiling .NET code with Mono compiler is almost similar to compile with g++/gcc. The basic steps are as follows.

1) Download and install the Mono for your platform from http://www.go-mono.com/mono-downloads/download.html
2) Then write the code (we are going to see how to use c# compiler) with your favorite editor or IDE.
3) Now to compile the code, we simply need to execute a file called mcs under the <installed location of mono>/bin, like this

mcs <filename>.cs -out:<filename>.exe

4) This will compile .cs file specified and produces the executable <filename>.exe

But this compilation would produce only console application. That is whenever you execute the application, a terminal or a command window will be opened automatically. We may need to specify one or two options to ask the compiler to compile in forms mode.

mcs <filename>.cs -out:<filename>.exe -target:winexe -r:System.Windows.Forms.dll -r:System.Drawing.dll

The additional options used in this case are, -target and -r. -target specifies compiler that it has to generate a winexe type of application rather than console. -r option refers to the references. System.Windows.Forms.dll and System.Drawing.dll are the most common references needed when we start building applications with GUI support.

NOTE: We can even build GUI applications in console mode, but the console/terminal window will show up and will not be closed until the application is running.

No comments: