Tuesday, December 4, 2007

C# and .Net on Linux

Compile and run C# command line and GUI applications on Linux

Shekhar Govindarajan

In the past issues of PCQuest we have talked much about MS .Net and C# programming language. .Net and its programming languages (C#, VB .Net, VC++ .Net, Visual J#, Python .Net, Perl .Net, COBOL .Net, etc) are available for only the Windows platform�Win 9x, ME, NT, 2000, XP, Win CE (for portables), etc. But a project named Mono has been introduced now. This is an effort to port .Net and its languages. As of now only C# has been ported to non-Windows platforms.

Libraries, compiler/interpreter and a runtime environment are the three essentials to write and run a program on a platform (OS + hardware architecture). Libraries make the writing of large programs easy by providing commonly-used routines in the form of classes and methods (functions). The .Net framework also has a number of libraries called class libraries. The compiler and the runtime are needed to convert a program into an executable and to run this executable respectively.

Mono packages the port of .Net library classes, a C# compiler and the .Net runtime�CLR (Common Language Runtime).

A graphical Image browser applications in C# running on LinuxMono claims to run on Linux, Solaris, FreeBSD and MS Windows. And besides the Intel x86 processors (486, Pentiums), it is said to support Sparc, PowerPC and StrongArm processors too.

Without getting into more theory, let�s see how Mono works on Linux. We�ll install Mono on PCQLinux 7.1, compile some C# programs and execute them. We are assuming that you are familiar with the .Net framework and the basics of C#.

Installation
Mount this month�s PCQuest CD and change to the directory system/cdrom/unltdos/linux/mono. Install all the RPMs found in this directory as

rpm -ivh *.rpm

Among other RPMs required by Mono, the Mono RPM named mono-0.12_baselabs-20020709.i386.rpm gets installed. The basic .Net assemblies like System.Data.dll, System.dll, System.Drawing.dll, System.Web.dll and System.Xml.dll are copied to the directory /usr/lib.

Now, type in the following program in a Linux text editor and save the file as HelloMono.cs.


class HelloMono
{
public static void Main(string[ ] args)
{ System.Console.WriteLine(�Hello Mono�);
}
}

Compile the program as

mcs HelloMono.cs

Mcs is the Mono C# command-line compiler. It has many command-line options like the csc compiler of MS .Net SDK. The above command will produce an executable named HelloMono. exe. Note that this executable is not a Linux executable but a .Net executable, technically called the IL (Intermediate Language) code. To run this executable, issue the following.

mono HelloMono.exe

You will see �Hello Mono� displayed on the Linux console. For more on mcs and Mono, refer to their man pages, which can be brought up by issuing:

man mcs
man mono

GUI on Mono
Mono does not support Windows Forms for GUI widgets. However, GTK# (a C# binding of GTK+graphical library) can be used for coding GUI components in C#. You�ll find all the requisites for installing GTK# on the CD. Let�s first install it and then execute some sample GTK# programs.

Change to the directory system/cdrom/unltdos/linux/gtksharp/dep on the CD and install the RPMs as

rpm -Uvh *.rpm --nodeps

Copy the file named gtk-sharp-0.2.1.tar.gz found in the directory system/cdrom/unltdoss/ linux/gtksharp to /opt directory. Extract the archive as

tar �zxvf gtk-sharp-0.2.1.tar.gz

This will produce a directory named gtk-sharp-0.2.1 within /opt. Change to this directory and issue

./configure --prefix=/usr
make
make install

Monkey Business
Money wise with Mono: .Net development can take place on a low-cost non-Windows platform like Linux. The deployment platform can be either Linux or Windows, with the OS cost (per user, per system licenses) in Windows concerning only the end user and not the developer.

Consider the scenario of a client-server .Net application. A user will interact only with the client (which should hopefully be a pleasant-looking GUI application) and expect a familiar platform like Windows. It will not matter to him if the server part is on Windows or Linux. So, the server part can be deployed on a Linux server using Mono, saving on the cost of setting up a dedicated Windows server.

Easy migration: If Mono comes out well, Windows developers, too, can develop for Linux and other non-Windows platforms. It won�t be a surprise if later a free or low-cost .Net IDE (Integrated Development Environment) like Visual Studio .Net makes a debut for Linux.

Linux developers, who should already have a grip of C/C++, can learn C# with similar syntax and constructs as in C/C++. Or, they can wait for a Mono C++ compiler that would give them an easier migration path.

The success of all this depends upon how well Mono is developed and how close and compatible it is with the implementation of the .Net framework on Windows. Other key factors will be its performance and the look-and-feel of its GUI components.

If you�re wondering what the title Monkey Business means, here�s the mystery unraveled�Mono means monkey in Spanish.

All the assemblies related to GTK# (gtk-sharp.dll, atk-sharp.dll, gdk-sharp.dll, glib-sharp.dll and pango-sharp.dll) are copied to the directory /usr/lib. You will need to refer to them using the �r option (see below) when you compile a C# program that uses GTK# for GUI. Reboot the machine once before proceeding further.

You�ll find some GTK# sample programs (HelloWorld.cs, Menu. cs, ButtonApp.cs) in the directory /opt/gtk-sharp-0.2.1/sample. You can compile them as

mcs -r gtk-sharp -r glib-sharp HelloWorld.cs

mcs -r gtk-sharp -r glib-sharp -r System.Drawing Menu.cs

mcs -r gtk-sharp -r glib-sharp -r System.Drawing ButtonApp.cs

Note how we are referring to the various assemblies using the �r option. Next, start X Window using the startx command and from within a terminal window, change (cd) to the directory /opt/gtk-sharp-0.2.1/sample and issue the following commands to execute the respective programs.

mono HelloWorld.exe
mono Menu.exe
mono ButtonApp.exe

Since Mono is an ongoing project and not all the .Net class libraries have been ported, you may find some quirks with it. Nevertheless, it�s usable to a great extent. You can check out the port status of class libraries at www.go-mono.com/class-status.html. For more on Mono, go to www.go-mono.com. For more on GTK#, see http://gtk-sharp. sourceforge.

Net/.

Shekhar Govindarajan

1 comment:

Air Cargo Packers And Movers said...

we provide best packers and movers service all over india and i am the regular reader at your site your posts are very genuine i also like your posts so please keep posting new updates
Packers and Movers Delhi

About Me

Ordinary People that spend much time in the box
Powered By Blogger