C# Compilation from the Command-line

.NET DevelopmentWhile Microsoft offers a powerful integrated development environment together with its .NET platform, it can occasionally be useful to compile code directly from the command line without the overhead of the Visual Studio IDE.  Luckily, every full .NET framework installation contains the “csc.exe” command line compilation program that can turn any Windows PC into a development machine.

The first step to leveraging C# command line compilation is to create simple C# .cs file.  Create a new folder for the project, and then a new file called “Program.cs” in that folder with a short sample code.  In this example, we’ll create a “Hello World” app:

using System;
 
namespace Program{
  public class Program{
    public static void Main(){
      Console.WriteLine("Hello World!");
    }
  }
}

When compiling a C# program, the entry point will be the static Main method, similar to the entry point for C/C++ programs.  Additional libraries can be included in the file header, and most libraries in the GAC (Global Assembly Cache) are supported by default.

Next, we will need to locate csc.exe, the C# compiler.  This is usually in the .NET framework version folder.  Navigate to the C drive, open the Windows folder, and then the Microsoft.NET subfolder.  Under the following Framework subfolder, there should be a folder for each .NET version number installed on the system.  If unsure of which .NET framework you would like to target, pick the latest version number that contains the csc.exe file.  Some incremental version upgrades might not have the csc.exe file in the folder – in that case, go to the next version down.  A sample path could be:

C:\windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

Next, create a batch command file called “make.cmd” in the same folder, and add the following line based on the csc.exe path:

for /r %%i in (*.cs) do c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe %%~ni.cs && %%~ni.exe

This batch file will compile each .cs file in the folder separately, and can be useful if you want to generate multiple executables from the same folder.

Finally, open a command prompt, navigate to the project folder, and type ”make” to compile.  This should compile a “Program.exe” file, and automatically run the program after compilation.

The advantage to setting up a command-line environment is that it only needs to be done once, and further projects can then be created by simply copying the environment to a new folder.  This technique can be useful for creating quick and simple short-term .NET apps, or even integrated into software to enable custom-compiled executables based on an interpreted language.

Written by Andrew Palczewski

About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.
Google+

RSS Twitter LinkedIn Facebook Email

Leave a Reply

Your email address will not be published. Required fields are marked *