Click here to Skip to main content
Email Password   helpLost your password?

Mono in Windows 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:

  1. Windows XP (Otherwise it wouldn't be Mono on Windows XP :) ).

  2. Mono

    More information is available at: http:www.go-mono.com

    Install Mono:

  3. 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

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

History

Version 1.0
This is the first version of this article.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionCompile of C++ code in XP Platform
kk_kumaraswamy
20:59 27 May '07  
Hi

i wanna know how to compile an C++ code in XP platform

Help me

With Regrads
KK
GeneralEver heard of #develop
kratchkov
10:54 1 Sep '04  
#develop is a pretty good IDE for C#. It does code completition and much more, and I suppose it works for Mono too.
Have a look at http://www.icsharpcode.net/OpenSource/SD/

And btw, it's being ported for linux, and uses Gtk# to render windows. The project is named monodevelop and you may find a link on Mono's site (www.go-mono.org)

----------------------
Instead of printing "No errors have been found", a compiler should rather print "None of your errors have been found", which would be, in most cases, true.

GeneralRe: Ever heard of #develop
Ryan Binns
18:09 13 Dec '04  
kratchkov wrote: #develop is a pretty good IDE for C#
It is absolutely the slowest editor I have ever used. I uninstalled it a few minutes after I installed it.

Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

GeneralRe: Ever heard of #develop
decuser
18:59 13 Dec '04  
Blame it on the .NET framework, not the ide...
GeneralRe: Ever heard of #develop
Ryan Binns
22:30 13 Dec '04  
I do, but the IDE was still unusable, so I uninstalled it.

Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

GeneralRe: Ever heard of #develop
SAKryukov
6:31 19 Mar '07  
Sorry, you're not right here about blaming .NET.
It is #develop edit control to be blamed.

Compare with the ColorTextBox contribution by Chrisi476 -- it's by far not perfect but much faster even with .Net GDI. After all, MS Visual Studio's editor is much faster, too.
This is unfortunate, because #develop is pretty robust in general.

SAKryukov

GeneralRe: Ever heard of #develop
Jon Davis
1:19 17 Dec '04  
Try upgrading your computer? The entire application is written in managed code, so it's not as optimized for speed as Visual Studio. Java IDEs written in Java have the same speed problem; the solution is just stop using that old 1GHz 256MB machine and get your hands on something with more meat (speed) and stronger bones (RAM).
GeneralRe: Ever heard of #develop
Anonymous
1:23 21 Dec '04  
I use it with my P3 450 machine and it works just fine. It does take a couple of seconds to load. Five seconds is more affordable than VS. Poke tongue
GeneralRe: Ever heard of #develop
Anonymous
17:51 12 Jan '05  
It doesn't matter. Crap is crap, no matter how much you dislike Microsoft and Visual Studio. For a free c# ide, I much prefer Borland's version, but I don't even know if its free anymore. #develop is great if you have no ide options. Horrible if you do.
GeneralVS.NET not required
LorenzoDV
14:21 13 May '04  
At the beginning of the article, you state that with Mono, Visual Studio .NET is not required. True, but VS is not required with the original Framework either!

VS is just an editor (a very complex one indeed). The compiler (csc) and the runtime (Framwork) are free, like mcs and monoenv are.
GeneralRe: VS.NET not required
Will Senn
14:43 13 May '04  
That is true. However, I was merely highlighting the fact that with Mono you don't need an IDE (the same could be said for the Framework, but I was writing about Mono) Smile

As to the complexity of VS as editor... nah, it's about as primitive as they come - on a par with a really ancient version of Ultra Edit, notepad on steroids. VS as an IDE on the other hand is quite cool - code completion, folding and insight are great for developers to just highlight a few of it's integrated features.

Will
GeneralRe: VS.NET not required
Codebender
15:21 3 Jun '04  
>The compiler (csc) and the runtime (Framework) are free, like mcs and monoenv are.

No, the Compiler and runtime are free as in beer, not Free, as in speech. Big difference.
GeneralRe: VS.NET not required
Will Senn
15:41 3 Jun '04  
Amen, brother!

It's difficult to believe that this is not obvious.

Will
GeneralRe: VS.NET not required
LorenzoDV
22:39 3 Jun '04  
Please explain yourself: I mean they are free of charge, not free software like Mono.
GeneralRe: VS.NET not required
Codebender
18:52 4 Jun '04  
I thought that I did, but okay. You said:

> [They] are free, like mcs and monoenv are

While they are free, they are not "Free like mono is." They're free with an 'f' not Free with an 'F', so to speak.
General'hello world' in ASP.NET
lesta
0:29 11 May '04  
Hi all,

Is there a chance to get 'hello world' running on mono in a web application. ?

specifically : How far have mono progressed in
- WebControls
- Databinding
- ADO.NET

I had a look at this 1.5 years ago. Pherhaps someone can tell us his experience with this.

Uwe Lesta
GeneralRe: 'hello world' in ASP.NET
caractacus
3:55 13 May '04  
http://www.go-mono.com/

They have progress reports, down to the method level.



caractacus
Generalwhat about this minimalist version
BlueBubu
14:15 9 May '04  
class _{static void Main(){System.Console.Write("hello, world\n");}}
GeneralRe: what about this minimalist version
Will Senn
14:45 9 May '04  
exactly Smile
GeneralGo Mono on Solaris
Amit K
4:28 8 May '04  
Hi,

Was just curious to know if MONO works on Solaris, as I could not find any mention of it on the site ( i noticed that u have one installed Poke tongue ).

Are there any build tools avaliable for Mono?


GeneralRe: Go Mono on Solaris
Will Senn
6:48 8 May '04  
I haven't built mono on solaris, but the faq says:

Question 111: What operating systems/CPUs do you support

Mono currently runs on Linux, Windows, Solaris, FreeBSD, HP-UX and MacOS X.

There is an ide in development for mono - called monodevelop oddly enough:

http://www.monodevelop.com/

It's a project to port SharpDevelop to GTK#:
http://www.icsharpcode.net/OpenSource/SD/Default.aspx
http://gtk-sharp.sourceforge.net/
http://www.nullenvoid.com/gtksharp/wiki/index.php

Later,

Will
GeneralRe: Go Mono on Solaris
jmw
6:56 9 May '04  
Given that .net is already freely available for Windows, I think the article would have been more interesting if you were able to demonstrate .Net running on Solaris or OS/X using Mono.

Having .Net be cross-platform is somewhat of a holy grail for C# developers -- and I think it's the true reason there's so much interest in Mono, not because it's another 'choice' on Windows.

Just my opinion...

GeneralRe: Go Mono on Solaris
Will Senn
9:15 9 May '04  
There are some practical considerations for the casual developer to explore using Mono in Windows - it's a 12 MB download, the runtime .NET framework - 30 MB. Mono is open source. Mono is supported by a very large community of developers. Mono isn't inextricably linked to IIS. Mono runs as a module in Apache. etc..

Perhaps, choice on Windows is not as interesting to you as it might be to some other folks.

As for running Mono on Solaris, OS/X or Linux - advocacy is well covered by the mono community itself - head over to go-mono.com for more on that.

Will
GeneralRe: Go Mono on Solaris
thawes
11:13 12 May '04  
I can testify, first hand, that Mono works on Solaris. Only 32 bit works, no 64 bit support, yet.
GeneralRe: Go Mono on Solaris
thawes
11:25 12 May '04  
... and, let me add, that the LDAP implementation is complete under Mono. I still remember the pain of trying to wrap the Novel LDAP C api with Managed C++ extensions over a year ago.


Last Updated 7 May 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010