linux poison RSS
linux poison Email

How to Compile and Run C# .NET application on Ubuntu - mono

Mono is a software platform designed to allow developers to easily create cross platform applications. It is an open source implementation of Microsoft's .Net Framework based on the ECMA standards for C# and the Common Language Runtime.

There are several components that make up Mono:
C# Compiler - The C# compiler is feature complete for compiling C# 1.0 and 2.0 (ECMA), and also contains many of the C# 3.0 features.

Mono Runtime - The runtime implements the ECMA Common Language Infrastructure (CLI). The runtime provides a Just-in-Time (JIT) compiler, an Ahead-of-Time compiler (AOT), a library loader, the garbage collector, a threading system and interoperability functionality.

Base Class Library - The Mono platform provides a comprehensive set of classes that provide a solid foundation to build applications on. These classes are compatible with Microsoft's .Net Framework classes.

Mono Class Library - Mono also provides many classes that go above and beyond the Base Class Library provided by Microsoft. These provide additional functionality that are useful, especially in building Linux applications. Some examples are classes for Gtk+, Zip files, LDAP, OpenGL, Cairo, POSIX, etc.

Installing Mono:
Open up the terminal Application > Accessories > Terminal and type following command
sudo apt-get install mono-xsp2 mono-xsp2-base
Console Hello World
To test the most basic functionality available, copy the following code into a file called hello.cs.
#include<stdio.h>
main()
{
   printf("Hello World\n");
   printf("My First C Program\n");
}

To compile, use gmcs: gmcs hello.cs
Here compiler will create "hello.exe"

Now execute the hello.ext using command: mono hello.exe
The program should run and output:
Hello Mono World


10 comments:

Anonymous said...

Hello! Nice blog. I would like to point out that the hello.cs code you have presented is not C#. It is C.

Riley said...

Thanks for this. I do my schoolwork on a netbook so I don't like running the IDE because it slows things down and is cramped on a tiny screen. This is perfect.

Anonymous said...

Is there a way to execute the CIL binary without type '$ mono hello.exe'? I want to execute just typing './hello.exe' on my Ubuntu.

Nikesh Jauhari said...

Write a simple bash script to execute your exe file.

Anonymous said...

If I write a bash script to execute, it will not be cross platform =/ I want to execute .exe files with the same command in DOS and bash: './hello.exe'

Nikesh Jauhari said...

In that case you can write a very simple java application and detect the OS and if it's Dos ... you can lunch .exe from the java application and if it;s linux/Unix .. you can use "mono foo.exe" to start you application.

At this point I can only think of this.
let me know if this works.

Anonymous said...

At this point, i'm using Python to execute binary file:

if plaftform.system() == 'Linux':
cmd = 'mono hello.exe'
elif platform.system() == 'Windows':
cmd = 'hello.exe'

And i'm using subprocess.Popen to run it in shell.
Thanks for help =)
If you find other way to do it without other language, post here, please.

Anonymous said...

why would I want to introduce a massive pile of crap ( .net ) to my linux box?

Anonymous said...

The sample program is C, not C#. If you're following this blog post, try this simple program instead:

using System;
using System.Text;

namespace TestApplication
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Testing");
}
}
}

Post a Comment

Related Posts with Thumbnails