Skip to main content

Mono brings .NET apps to Linux

Build applications for Linux while maintaining cross-platform capabilities using .NET-compatible languages

Eli Dow, Software Engineer, IBM Linux Test and Integration Center
Eli Dow is a Software Engineer in the IBM Linux Test and Integration Center in Poughkeepsie, NY. He holds a B.S. degree in Computer Science and Psychology and a Masters of Computer Science from Clarkson University. His interests include the GNOME desktop, human computer interaction, and Linux systems programming. You can contact Eli at emdow@us.ibm.com.

Summary:  Mono, the open source development platform based on .NET, lets you build powerful, flexible Linux® applications and still take advantage of cross-platform capabilities using a variety of .NET-compatible languages. This article walks you through installing Mono on your system and developing your first sample Mono-compiled C# application that runs on both Linux and Microsoft ® Windows®.

Date:  19 Sep 2005 (Published 15 Sep 2005)
Level:  Introductory
Activity:  3570 views

The C# language is an object-oriented language designed to let you quickly build a range of applications for the Microsoft .NET platform. The goal of C# and .NET is to free you from low-level programming issues -- type-safety issues, memory management, library construction, etc. -- in order to allow you to concentrate on building your applications and focus on business logic. This, in turn, should help shorten the development cycle.

With all the buzz around the C# and .NET strategy, some might wonder what the Linux community is doing in reaction. This article discusses how the open source world (with strong backing from Novell) has chosen to embrace the language by implementing its own compiler and class libraries. (In fact, several open source implementations are underway.)

This article focuses on Mono, the open source development platform based on the .NET framework and sponsored by Novell. This article takes you through installing Mono on your system and gets you started developing your first sample C# application, compiled with Mono, that runs on both Microsoft Windows and Linux.

Mondo Mono benefits

Mono gives open source developers the programming power and flexibility to build applications for Linux while maintaining cross-platform capabilities, using a variety of .NET-compatible languages. One of the great advantages of Mono for current .NET developers is providing an easier migration path to Linux. The Mono project has a very open and active development community and provides both developer tools and the infrastructure needed to run .NET client and server applications.

Perhaps the most important benefit of using the Mono architecture is that you gain language independence. Mono lets you leverage any existing code from languages supported in the .NET runtime.

Consider the following: traditional Linux programming is often done entirely in C. Normally, when you want to use another language, such as Python, you need to use the Python bindings for the native C code.

This approach, creating bindings, is not always optimal or easy to do, and in some cases, no bindings exist; therefore, from any one Mono-supported language, Mono allows you to access existing code from any other Mono-supported language. You can write applications in C#, Python, or even Java™ while reusing classes from the other languages without the need for bindings! (An example of using C library code from a C# program via the Platform Invocation Facility [pinvoke] is a bit later in this article.)

As mentioned above, your code also gains platform independence. This happens automatically because .NET code is not compiled to machine code; rather, it is compiled to intermediate language (IL). The IL used in the .NET runtime that is provided by Mono makes it possible to copy binaries across platforms. This idea is quite familiar to Java developers -- it echoes the concept of "write once, run anywhere."

Mono also provides a time-saving free implementation of the .NET framework class library. This library is already written and tested code designed for a variety of common programming tasks (as well as for some that are not so common). The library includes most common data structures, cryptographic libraries, XML, and Web services support.

Though many languages can be used within the .NET framework, C# is the one held in highest esteem by .NET developers. C# is an effective, garbage-collected language that provides powerful object-oriented capabilities and allows you to get to the core of your programming tasks quickly. The language has a syntax familiar to almost anyone who has programmed in a modern C-derived language, but includes convenience features familiar to those using modern scripting languages as well.

Lastly the Mono JIT (Just In Time) compiler is speedy, providing all these benefits at a reasonable performance level.


More on the workings of Mono

The Mono project includes the following open source implementations:

  • A C# compiler
  • The Virtual Execution System, including a JIT compiler, garbage collector, loader, and threading engine
  • An implementation of the .NET class library
  • Visual development tools and debuggers

The Mono project's tools can be run on a variety of hardware and operating systems, including Linux, Windows, and Mac OS X.

The Mono project currently supports the following list of languages to varying degrees:

  • C#
  • Java
  • Boo
  • Nemerle
  • Visual Basic.NET
  • Python
  • JavaScript
  • Oberon
  • PHP
  • Object Pascal
  • And more

Note that not all of these are as complete as the C# runtime. For example, Java isn't supported directly but uses on-the-fly translations from Java bytecodes to IL.

Another example of the differences in the implementation completeness is that the Python implementation seems to have stalled recently. Developers seeking python might want to use Boo, which is a more complete scripting language implementation (while being similar to Python and designed for the .NET framework). While discussing Mono-supported languages, I should mention that Nemerle is a new hybrid programming language (functional, object-oriented, and imperative) for the .NET platform.

Some political issues

Some people are confused as to why open source advocates would attempt to take a Microsoft product and embrace it. In fact, many wonder why Microsoft would allow this in the first place.

The reason this is possible, and as it turns out even encouraged, is that Microsoft has followed through on its promises to standardize C# and the CLI. Because of that standardization and specification, the Mono team has been able to construct their own open implementation of the ECMA standard for C# and the Common Language Infrastructure. In fact, the compiler and language themselves are openly specified for others to implement as well.

A lot of the fuss about Mono arises more from the discussion of the provided class libraries that are not necessarily covered as an open specification. This invariably leads to flamewars about patents and whether or not Microsoft will try to use patents to attack Mono as part of a broader strategy of attacking Linux. To that end, the Mono developers provide two independent sets of libraries.

One library set is a reimplementation of the Microsoft .NET libraries used for compatibility with Microsoft .NET applications. This includes ADO.NET, System.Windows.Forms, and ASP.NET.

The Mono developers have also created their own powerful stack of libraries from the ground up, based on existing open source libraries and technologies with the intent of providing functional parity while being free from any concerns of patent infringement. For example, Mono ships with a windowing technology called Gtk#, a set of C# bindings for the gtk+ toolkit, and assorted GNOME libraries. This enables application developers to write fully native GNOME application using Mono. You can consider this library the open source parallel of Microsoft's System.Windows.Forms library.

What platforms does Mono run on?

Mono has been successfully ported to Linux on multiple hardware platforms, FreeBSD, Windows, and Mac OS X. It is estimated that this covers 99 percent of the desktop market.

Table 1. Mono platforms
PlatformInterpreterJitter (faster)
Linux/AlphaYesNo
Linux/ARMYesYes
Linux/MIPSYesNo
Linux (PPC)YesYes
Linux (S390)YesYes
Linux/SPARCYesYes
Linux (x86)YesYes
Linux/x86-64 (64-bit port)YesYes
FreeBSD (x86)YesYes
HP-UX/HPPAYesNo
MacOS XYesYes
Solaris/SPARC v8YesYes
Solaris/SPARC v9 (64-bit port)YesYes
Windows 2000/XPYesYes

Installing Mono

Now, let's look at some of the nuances in installing Mono.

Mono on Windows

To install Mono on the Windows operating system, you need to be running Windows 2000 or Windows XP. To begin, download the latest editions of the Mono Win32 installer from the Mono Windows Integration Project home page hosted at NovellForge (see Resources at the end of this article). When the download has completed, run the executable installer.

The installation process is similar to other product installs. Initially, you will be asked to accept or decline a license. After accepting, you need only provide the installation path and select any optional components.

For the purpose of learning the Mono frameworks, it is recommended that you use the default installation, which provides start menu entries for the common tools. These are categorized under a new "Mono-1.1.7 for Windows" program group.

Mono on Linux

Installing Mono for Linux should be relatively easy in most cases (the specifics of obtaining packages for each and every distribution is beyond the scope of this article).

Binary installation packages are readily available for SUSE, Red Hat, and Ubuntu, as well as many others. Source-based distributions like Gentoo are also easy to get up and running with minimal effort.


Testing the Mono installation

To test the core compiler (mcs) as well as the runtime (mono), you should create a simple program and compile it. You may create the program in any text editor you prefer. For a quick and dirty way of creating this file (albeit without any elegant formatting), run the following command from your terminal prompt (all on one line):

$ echo 'class X { static void Main () { System.Console.Write("My first
mono app worked!\n");} }' > example.cs
.

This command creates a C# source file called example.cs (you can also download this file, example.cs, and the executable, example.exe from Downloads, below). (Note that if you are using Linux, you may simply use your bash prompt; if you are using Windows, invoke the Mono command prompt from the start menu.)

To test the compiler's ability to create executables, invoke the following command:

$ mcs example.cs

This should leave you with a binary called example.exe. To run this binary and thus test the runtime, use this command:

$ mono example.exe

If all went well, you should see "My first mono app worked!" in your console with no errors.


Figure 1. What you'll see if all went well
What you see if all went well

In fact, you can copy the resulting executable file to another system, perhaps one running Windows, and then execute it there, unmodified.


Using code from a non-Mono library

One of the more compelling reasons to use the Mono platform is the ability to use code from an existing, possibly non-C#, library from your C# code. Here is an example of doing just that (you can also download Listing 2, PInvokeExample.cs, and the executable, PInvokeExample.exe, from the Downloads section, below).

The mechanism that provides this capability is the Platform Invocation Facility (or pinvoke, for short).


Listing 2. Using the Platform Invocation Facility
/* Platform Invocation Facility Example
   This code is intended to illustrate P/Invoke.
   For out purposes we will access a c shared library.
*/

using System;
// This is a lot of the magic!
using System.Runtime.InteropServices;
/* Class illustrates pinvoking existing c library code */
public class PInvokeExample
{
   /* Notifying the runtime that we need an
          additional mathematics library, libm.so */
   [DllImport("libm")]

   /* Here we define the external function we will
           be using from the library,

           You must declare these methods to be static.
      The function signature you provide must match the
           signature in the external library!
   */
        static extern double sqrt ( double element );


   public static void Main ()
   {
      Console.WriteLine("The square root of 100.0 is {0}.", sqrt(100.0));
   }
}

As you can see in the simplified code, you need only direct the Mono compiler to use the library (this was done with the DLLImport line) and provide the prototype for the function you wish to use. If you compile this class on a Linux system, the console output should show the correct answer.


Figure 2. Using non-C# libraries
Using non-C# libraries

Some final Mono benefits

Mono's runtime can also be embedded into applications for simplified packaging and shipping. In addition, the Mono project offers an integrated development environment, debugger, and documentation browser.

If you are interested in learning more about C# and the .NET framework, a variety of Resources are listed below, including references to various integrated development environments like Eclipse and Monodevelop to further simplify your development process.

If you are interested in developing graphical applications with Mono, I highly recommend "Mono: A Developers Notebook" as a more in-depth tutorial (Resources).

The programmers at Novell are continuously adding new functionality to Mono and providing tools to make open source developers' lives easier. I hope that this article gives you with a solid introduction to the Mono project and that you can use it in your next Linux development project.



Downloads

DescriptionNameSizeDownload method
Installation test sourceexample.cs1 KB HTTP
Installation test executableexample.exe3 KB HTTP
PInvoke sample sourcePInvokeExample.cs1 KB HTTP
PInvoke sample executablePInvokeExample.exe3 KB HTTP

Information about download methods


Resources

Learn

Get products and technologies

  • Download Mono for various platforms from the Mono Project downloads page.

  • Eclipse for developing Mono applications gives information on using the Eclipse IDE for C# and other Mono languages.

  • DotGNU provides free software solutions for Web services and C# programs.

  • MonoDevelop is a project to port SharpDevelop to Gtk# to create a best-of-breed development environment for UNIX systems for C# and Mono.

  • Gtk# contains a set of C# bindings for the gtk+ toolkit and assorted GNOME libraries.

  • The Mono Windows Integration Project is developing Mono and its tool set for use on Windows.

  • Build your next development project on Linux with IBM trial software, available for download directly from developerWorks.


Discuss

About the author

Eli Dow is a Software Engineer in the IBM Linux Test and Integration Center in Poughkeepsie, NY. He holds a B.S. degree in Computer Science and Psychology and a Masters of Computer Science from Clarkson University. His interests include the GNOME desktop, human computer interaction, and Linux systems programming. You can contact Eli at emdow@us.ibm.com.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux, Open source
ArticleID=94015
ArticleTitle=Mono brings .NET apps to Linux
publish-date=09192005
author1-email=emdow@us.ibm.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers