Compile and run C# in the Command Line (Linux, Mac & Windows)

Published in 04-09-2016 by Luis Lopez

Last semester at my uni I had a subject called Object Oriented Programming where our teacher told us we were going to be using C# (C Sharp) as our language for the class. It meant that we had to find a way to install Visual Studio and get things done but the main problem was with those who had a Mac or me, the only one who dared to have a fresh Elementary OS installation (dual booted with Windows 10) into class. For the Mac people, it meant that they didn’t know how what tool to use apart from X-Code which of course didn’t work (but it did for C++) and me, it meant that I had to find a way to compile my code written in mere text editors like Atom or Sublime.

Thankfully I found three ways I could catch up with my classmates during our lab classes: REPL.it (an online tool for writing in a plethora of languages and seeing the STDOUT result), the CSC (C-Sharp compiler) command line tool in Windows and Mono (compiled from the Terminal or with MonoDevelop which is a cross-platform IDE).

Sponsored by Microsoft, Mono is an open-source implementation of Microsoft’s .NET Framework based on the ECMA standards for C# and the Common Language Runtime. A growing family of solutions and an active and enthusiastic contributing community is helping position Mono to become the leading choice for development of cross platform applications. - Mono’s Website

Compiling C# on Windows

Since this is Windows we’re talking about, you probably have the .NET Framework already installed on your computer; the usual path to it is C:/Windows/Microsoft.NET/Framework, inside you’ll see a bunch of folders with version names. Navigate to the version number that you want to work with (I always choose 4.x) and copy the absolute path of the folder by clicking on the address bar and copying its contents.

Now, you have to edit the PATH environment variable and add the directory address you just copied. This is easy on Windows 7+ because all you have to do is open the search box and typing “variable”, you’ll see something like “Edit System Environment Variables”, click on it and you’ll see something like this:

EnvVar

Click on PATH (sometimes it’s “Path”) and click on the Edit button, in the “Variable Value” text box, at the very end, if it doesn’t end with a semicolon, insert one and paste the address you copied.

Hit OK on both windows, exit the system configuration dialogs and then open the command line (Win + R and type cmd, then click on Run) and type in csc, if it says that the command isn’t recognized, you’re doing things wrong. Install the .NET Framework if you haven’t, localize the direct path to the latest version where the csc.exe executable is and copy the path, then follow the PATH variable instructions here from HowToGeek. Otherwise, you’ll be welcomed with the Microsoft C# Compiler and it’s copyright details.

Now, create a C# test file, it can be this:

sourcefile.cs
1
2
3
4
5
6
7
using System;
class HelloWorldWin {
static void Main() {
Console.WriteLine("I came from the CSC compiler :D");
}
}

Run the following command in the same directory as the file:

1
csc /out:program_name.exe source_file.cs

Once you fix any compilation warnings and errors that the compiler may throw at you, you won’t see any output but it means that you can now run the code by typing the name of the executable (adding .exe is optional), hit ENTER and you’ll see your console output! CSC has some options that you can use, for example, the file wildcard for compiling more than one C# files into a single executable by substituting source_file.cs with *.cs, also, refer to the documentation.

Compiling and running C# on Linux and Mac

You can run a GUI application and a console app using Mono, but to actually run ASP .NET web applications you need more than just Mono, you need the K Runtime Environment, I’m sorry I can’t help you with that (I haven’t even bothered to learn ASP .NET ─yet─) but I can refer you to a good and quick tutorial to get install it and get a .NET application running and its dependencies installed; click here for the TechRepublic tutorial. I also saw .NET Core being suggested so you may want to check that out.

Step 1: Install Mono

For Mac users, I won’t say more than this: all you have to do is download the .pkg file, skip to the next step and get going. Now, for the Linux users, you can go here and follow the instructions which I’m going to mention next anyway. Note that the instructions I’ll paste are valid for Ubuntu and Debian based distros (I’m using Elementary OS so, I had to choose this) but if you’re running CentOS, Fedora (or any of its derivatives) or OpenSUSE and SLES you better click on the link I mentioned a couple of sentences ago.

Open up your terminal window and type in the following command, which is going to add a PPA key, you don’t need to worry about what this means if you are new to package management in Linux:

1
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

The following command will append a new line to our source list file so that apt-get can find the package instead of outputting a message saying it couldn’t find it:

1
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

Now, by rule of thumb, every time we modify the source list or intend to install a new package, we have to run a command to download the package lists from the repositories and update them to the latest versions (it will probably ask for your user password, type it in and hit ENTER):

1
sudo apt-get update

Finally, it’s time to install Mono itself and all its dependencies, when you run the command you’ll see that it will ask you to download a ton of packages, it’s ok. What we’re doing here is tell apt-get to retrieve and install all of the packages mentioned; this process is detailed here.

1
sudo apt-get install mono-complete

Step 2: Compiling and running C# code

To make sure mono is working correctly, why don’t we set an example console STDOUT application? A simple “Hello World” will do the trick. Open the terminal and navigate to any directory you want, then create a file called hello.cs by typing touch hello.cs or editing directly into VIM or Nano and then save the file. Here are the contents of said file:

hello.cs
1
2
3
4
5
6
7
using System;
class HelloWorld {
static void Main() {
Console.WriteLine("Hello from CodeTuts!");
}
}

Now, save it and in the terminal window (making sure you’re still in the same directory as the C# file), execute the Mono compiler; now, there are three compilers that you should be aware of according to the Documentation:

  • mcs: references the 4.0-profile libraries (the APIs as defined in .NET 4.0) and supports C# 4.0.
  • gmcs: references the 2.0-profile libraries (the APIs as defined in .NET 2.0 and .NET 3.5) and exposes the full C# 3.0 language.
  • smcs: references the 2.1-profile libraries (the APIs defined for Silverlight) and exposes the full C# 3.0 language. This is the compiler used for creating Silverlight/Moonlight applications.

dmcs has now been deprecated and the compiler tells us to use mcs instead, so this is the one I’m using!

1
mcs -out:hello.exe hello.cs

This command will use the mcs compiler and create a Windows executable with the name of “hello”, taking from source hello.cs. If the compiler has no warnings or errors to tell you about, you won’t see any output, why don’t you try to remove the semicolon from line 5 on the file? It will output something like this:

1
2
hello.cs(6,3): error CS1002: ; expected
Compilation failed: 1 error(s), 0 warnings

It’s telling us that there’s an error on line 6, column 3 (it says 6 and not 5 because of the missing semicolon, it thinks the line continues up to the curly brace) and the message is ; expected which is pretty self-explanatory. If you want to see all the options available in the compiler, why don’t you head up to the official documentation? One last tip, if you want to compile all the files in a folder you can use the wildcard mcs -out:my-program.exe *.cs asterisk.

But what if I want to compile a Windows desktop app with GUI that I built with Windows Forms? Then you’d have reference the appropriate DLLs and then run the following command:

1
mcs -out:my-gui-program.exe -r:System.Windows.Forms.dll -r:System.Drawing.dll *.cs

Note that you can also start building with GTK, refer to the documentation for how to build GUIs with GTK using Mono.

Time to run the code

The mono command will allow you to execute the code we just created in the terminal or even a desktop GUI app (for web apps, that’s a whole different shabang). Open the terminal if you closed it by accident and navigate to the directory where the C# file was. Now, execute the following command:

1
mono hello.exe

You’ll see that the output is indeed “Hello from CodeTuts!”:

1
2
unix-username@device-name:~/your-project-path$ mono hello.exe
Hello from CodeTuts!

Enter OmniSharp and MonoDevelop

OmniSharp is a server that will enable your code editor to have the properties of a C# IDE (still not comparable to a real IDE though) and you can also install Yeoman to use some pretty neat project generators (scaffolding). For SublimeText you can go ahead and use the package installer and fetch OmniSharp. If you’re running Atom (the experience is more pleasant here) you can install the package with the same name omnisharp-atom (via the app or using apm). For more information go to the official website or watch this video. For Omnisharp to work you need Mono 3 dot something and bigger, I am running the 4th version and still couldn’t get it to work on Elementary OS, I’m not sure if I’m stupid or if I am missing something but I really hope you can get it to work because it’s a blessing because it provides IntelliSense, errors and warnings, code autocompletion and more.

Omnisharp

All due image credits go to Quique Fernández at EsMSDN’s blog.

What about the IDEs?

As I mentioned somewhere in this tutorial, you can install two IDEs that are cross-platform and compatible with Mono. MonoDevelop works with C# and F# (functional programming language by Microsoft) and can be installed on the 3 major platforms.

MonoDevelop

And Xamarin is a mobile .NET platform, you can create Android, iOS and Windows Apps with it using C#.

Xamarin

Also, if you’re on Windows and can’t afford Visual Studio Professional, you can always rely on Visual Studio Community. Another good editor you can use is called VS Code, which can make your transition a bit more comfortable.

Conclusion

Whatever method or platform you use, make sure it’s what you need, I’m still very new to C# and I haven’t even looked into what ASP .NET has to offer because I’m too tangled into the cool kids’ stacks like MERN (MongoDB, Express, ReactJS, and NodeJS) or LEPP (Linux, Nginx, PostgreSQL, and PHP).

Did you know that there’s a C# interactive console? I didn’t, I just found out today while writing this thing. Type csharp in the terminal if you have Mono installed and enjoy!

Optional: Build with Sublime Text

I almost forgot! You can take advantage of Sublime Text’s builds, they are just shell commands you can execute from within ST by running the CTRL/CMD + B key combination (or going to Tools > Build).

To get started, go to the Tools menu and hit the Build System submenu, then click on New Build System.... You will be received with a new file that looks like a JSON literal, change the content of that file with this:

For Linux and MacOS
1
2
3
4
{
// Multiple files: "dmcs -out:compiled.exe *.cs && mono compiled.exe"
"shell_cmd": "dmcs -out:$file.exe $file && mono $file.exe"
}
For Windows Machines
1
2
3
4
5
{
// Multiple files: "csc /out:compiled.exe *.cs && compiled.exe"
"cmd": "csc /out:$file.exe $file && $file.exe",
"shell": true
}

Now, save the file with the name of csharp.sublime-build in the suggested directory and now go to a c# file with Sublime Text, navigate to the Tools > Build submenu and click the “csharp” checkmark, now when you execute the Build command (CTRL/CMD + B) you’ll see a mini window down below like this:

Sublime Build

There’s also a plugin called Script! for the Atom editor but it’s harder to configure as it uses csc as its default command for executing C# code.


Comments: