HelloMono - Mono on Windows XP
The Monkey on XP
Introduction
If you think that you have to have Visual Studio .NET in order to develop in .NET, you are in for a pleasant surprise. Mono - in Spanish the word means monkey, in geek-speak it means choice. Either way, it is most assuredly a good thing. Mono is an open source, free implementation of the .NET Framework. On May 4, 2004, the Mono project released the first public beta of their work.
A few days after I completed my previous article - hello, world - a primitive view of the state of the art, the Mono Beta 1 was released, and I wanted so badly to include a HelloMono example in my article. The only problem was that I absolutely couldn't get Mono to work on my system. Now, I am about as tech savvy as they come. I run Windows XP, Windows 2000, Solaris 10 (Software Express), Libranet, FreeBSD, and Whitebox (basically Redhat Enterprise), and occasionally a half dozen other OSes, all on the same box. When in Windows, I have Cygwin with XFree86 available and in constant use. I thought Mono would be a breeze to setup, after all it has a windows installer. Not so, I am afraid. However, all hope is not lost - it is a few days later and I have figured out how to make Mono purr, and it runs fine. Thanks to the open source community, I was able to add a bug report to the project and while the bug is still open, some kind soul posted a reasonable workaround that got me up and running.
First, here is what got me started on the whole hello, world kick again after all of these years. Kernighan and Ritchie's ubiquitous "The C Programming Language" is considered by many to be the most influential programming book of our time. Written in 1978, it immortalized the now well known phrase - "hello, world". The book states that "hello, world" is the basic hurdle to learning all languages: "to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy."
You need to have some perspective to truly appreciate this statement or you might be tempted to consider it overreaching. Dennis Ritchie designed the C programming language and it was the C language that made portability possible for the Unix operating system. Unix and Linux, Minix, BSD, OSX, YouNameItOS and even Windows owe a great debt of gratitude to the language. Even the language C# is a derivative of Ritchie's C. So when in his book, it says that "hello, world" is important, we should listen.
So it is that I have taken my inspiration from K&R's work. I can think of no better method to expose the foundations of the development environment even in the year 2004 than the old, "hello, world" approach.
This article is a "hello, world" tutorial for Mono running on Windows XP:
Background
A basic understanding of .NET, the command-line, and working in Windows is assumed. Here are some good resources to help you get up to speed on these topics:
There are basically three required pieces to doing .NET development using Mono on Windows XP:
- Windows XP (Otherwise it wouldn't be Mono on Windows XP :) ).
- Mono
More information is available at: http:www.go-mono.com
Install Mono:
- Download and install Mono using the Windows Installer
- Create a monoenv.bat file in the Windows directory or somewhere else on the Path (not required, but it will make your life much easier)
@echo off set MONO_PATH=d:\mono\lib set PATH=d:\mono\bin;d:\mono\lib;d:\ikvm\bin;d:\windows;
d:\j2sdk1.4.2_03\bin echo mono environment established - Open a command prompt to test the installation
monoenv mono environment established mcs error CS2008: No files to compile were specified Compilation failed: 1 error(s), 0 warnings
If the testing fails because of a System Assembly not found error - Using Explorer find all .dll files in the mono\lib\mono\gac directory and below and copy them to mono\lib
- run mono, should print help
mono Usage is: mono [options] assembly Runtime and JIT debugging: --compile METHOD Just compile METHOD in assembly --ncompile N Number of times to compile METHOD
(default: 1) --regression Runs the regression test contained
in the assembly ...
- UltraEdit
Well, ok, it is not a requirement, but it will sure make things easier. Get it at http://ultraedit.com/. If you do not have a decent text editor, Notepad will work ok.
You should now be set up to develop with the Mono on the command-line.
HelloMono
This is a very simple tutorial and does not pretend to exercise Mono to its fullest.
OK, let's get started. What are the goals? Borrowing directly from Kernighan and Ritchie, these will be our goals for this tutorial:
- Create the program text
- Compile the program
- Load the program
- Run the program
- Locate the Output
Create the program text
In order to create the program text, you will need to use a text editor of some sort. I prefer using UltraEdit, although Textpad or Notepad will work as well. Do not even bother with Word or Wordpad, these are arguably word processors and they tend to do funny things to text files. Fire up the editor and you will be ready to begin.
Here is K&R's "hello, world" C program in all its splendor, it is the model I am replicating, so I feel it is fitting to include it here:
// reader Ryan Beesley noticed that the original quote lacked an include
// needed to compile so I added the following include - it is not part
// of the original K&R hello, world program
#include "stdio.h"
main()
{
printf("hello, world\n");
}
The C#.NET code that is required to print "hello, world" to the console is similarly simple:
1 using System;
2
3 namespace mynamespace {
4 public class HelloWorld {
5 public static void Main(string [] args) {
6 Console.WriteLine("hello, world");
7 }
8 }
9 }
Line 1 This is a using
directive that tells the compiler that we will be referencing objects that are included in the System
namespace
. According to Microsoft, "The System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions." For our purposes, the System
namespace
provides the handy, dandy Console
object that will allow us to print "hello, world" to the console.
Line 2 This is an empty line :) White-space, space, tab, carriage return, etc. in code (not strings) is generally considered insignificant, I tend to think of white-space as condensing to a single space or blank line.
Line 3 This is a namespace
directive that tells the compiler to segregate our code in its own namespace
semantically. This is not really required here, but it is good form. If you put any classes that you create in your own namespace
, you will not be bitten by namespace collisions down the road. A namespace collision is what happens when there exist two semantic elements that have the same name. For instance, let's say that I create a class
called Car
and somewhere else within the same project someone else also creates a Car
class. If I include both classes in my project, their names will collide unless they are in their own namespaces.
Line 4 This is the class
declaration, our class is publicly accessible and is called HelloWorld
Line 5 The Main
method. Console applications are required to have a Main
method. The Main
method is the method that will be executed by the operating system when the program is loaded and run. Main
is public
and static
, it does not return anything and it takes an array of strings as arguments. static
basically means that the method can be called without instantiating a HelloWorld
object. The arguments are include by convention, we will not be passing in any arguments.
Line 6 "hello, world" gets printed by this call to the Console
object's WriteLine
method which writes the argument to the console followed by a carriage return.
Line 7 End of the Main
method.
Line 8 End of the HelloWorld
class
definition
Line 9 End of the namespace
Save the file to your hard drive as HelloMono.cs and open a command prompt.
Compile the program
Compile the application using the Mono C# compiler:
mcs HelloMono.cs
This should produce output similar to the following, without error.
Compilation succeeded
Compiling the application will generate an executable named HelloMono.exe
Load and run the program
Loading and running the program are accomplished in a single step.
Load and run the application from the command-line by typing:mono HelloMono.exe
Locate the Output
Console output is incredibly easy to find. Generally, it appears on the line immediately following the command itself. Running the program should produce the following output:
hello, world
Congratulations, you have completed the HelloMono tutorial. Now I know what some of you are thinking, "It took nine lines to produce the same output K&R did in four? Ha! Gotcha, I knew .NET was lame." Well, it is not that simple. Here is a minimalist C#.NET HelloWorld console example for the purist:
1 class HelloWorld {
2 public static void Main(string [] args) {
3 System.Console.WriteLine("hello, world");
4 }
5 }
It is still bigger than the K&R version, but not by much.
Conclusion
That's it, really - not radically different than the same example using the .NET Framework. Give Mono a shot, it is worth a little investment of time to have a choice.
Email the author Will Senn
References
Kernighan, B.,& Ritchie, D. (1978). The C Programming Language (pp. 5-6). New Jersey, Prentice-Hall.
Resources
Background Resources
General Resources
- Copernic Agent Basic (free) is the most powerful search engine I have ever used
- Download source - 2 Kb
- Email the author Will Senn
- Google is the place on the web where you will have the most luck
- UltraEdit
History
Version 1.0
This is the first version of this article.