Monday, June 8, 2015

C# under Linux - Installation and First Steps

In the next posts I want to write about the operating system Linux and show how to develop and deploy C# programs there.
This is possible with the software Mono, which can quickly and without much previous knowledge be set up.
Mono is a .Net development and runtime environment for different platforms, amongst which are Linux and Android. Currenty the project is managed by the company Xamarin, in my posting series about app development with C# for Android I already wrote about Xamarin Studio, which is of course based on the Mono kernel.
A great advantage of Mono is, that .Net programs which have been compiled under Windows, can be run under Linux without the need of recompiling (and of course vice versa), so the execution really is platform independent.

Let us come to the installation, which I will describe for the Linux distribution Ubuntu. The only differences between the distributions are during the process of installation, from then on the usage etc. of Mono is the same. Basic knowledge about the usage of Linux is assumed.
The essential Mono components, like the runtime environemnt and the compiler, can be installed via the following input to the terminal:

sudo apt-get install mono-complete

If this operation executes without errors: Congratulations, your Linux now can speak .Net, especially C#!

Before writing in the next posts about the usage of a comfortable development environment, we here first test some basic functions.
A nice feature is the program csharp. If one enters this command in the terminal, an interactive C# shell is started, in which one can enter C# commands, which looks as follows:
















We leave the shell with the command quit.

Now I want to show the usage of the Mono compiler and its execution via the command line, similar to the usage of the C++ compiler gcc.
For this we write a C# program into a .cs file and compile this to an .exe file - done (here you can see nicely, how compact one can keep a C# program and what it is actually made of - contrary to the development with Visual Studio etc., during which many files are created).
So we first create the file HelloWorld.cs. We can do this for example through the terminal. Therefor, we navigate via cd to the desired directory and then enter the command vim HelloWorld.cs. vim is a text editor I like to use most, with the command we tell the computer to open the file HelloWorld.cs with vim - and since it is not yet existing, it is created (if the file is saved for the first time).
A new vim instance is opened, in it we input the following code (vim can be a little tricky when first used - here a good tutorial can be found - and again as a reminder, of course any possible text editor can be used):

using System;

public class HelloWorld
{
        static public void Main()
        {
                Console.WriteLine("Hello from the compiler!");
        }
}

We leave the file and save it via Esc - :x.
Then we call the compiler mcs:

mcs HelloWorld.cs

This compiles the file and creates, if no errors occur, the file HelloWorld.exe in the same directory.
We can execute this via mono HelloWorld.exe or simply by:

./HelloWorld.exe

And already we have created our first C# program with Linux and the compiler greets us.

About this you can also watch a video here.

No comments:

Post a Comment