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).
Mono 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
|
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