Notes on Building C# Code in Windows

2014-08-27 16:23

Even though many people probably don't think so it is actually possible to code and compile programs in Windows without having Visual Studio installed.

When you build a program in Visual Studio it makes use of a hosted version of MSBuild which in turn uses the Microsoft C# Compiler csc.exe.

MSBuild is a build engine like Make that uses build instructions written in XML. Microsoft claims that using the same build files the result from building from within Visual Studio and building using MSBuild from the command line produces identical results. I have found this to be true with a twist: when building web projects there are some build targets for deploy that are only exposed when building from within Visual Studio.

MSBuild can be downloaded and installed as a part of MSBuild Tools.

As far as I can tell MSBuild and csc.exe are also included in the .NET Framework so if you have that installed, and who hasn't, you probably don't need to install MSBuild Tools.

Hello World

For these notes to make sense I will use the following code, saved in a file hw.cs, to demonstrate.

class HelloWorld {
    static void Main() {
        System.Console.WriteLine("Hello World!");
    }
}

Using csc.exe

This is as simple as using a compiler on any other platform. The trick is to locate the compiler on your system.

If using csc.exe from MSBuild Tools it can be found in one of the following locations:

  • %ProgramFiles%\Program Files\MSBuild\<version>\Bin\csc.exe
  • %ProgramFiles(x86)%\Program Files\MSBuild\<version>\Bin\csc.exe

If using csc.exe from .NET Framework it will be found in:

  • %SystemRoot%\Microsoft.NET\Framework\<version>\csc.exe

To build a program just call the compiler with the code file as argument:

csc.exe hw.cs

Now you will have the resulting executable hw.exe.

If more than one source code file is involved you need to specify all of them on the command line. Use the compiler option /output to control the name of the resulting executable.

For more information about options to csc.exe see C# Compiler Options.

Build a stand alone executable

A lot of times it is easier to distribute a single executable than create and maintain a full installer program. This can be accomplished by taking advantage of the /reference option in the C# compiler.

Using MSBuild

MSBuild.exe can be found in the same location as csc.exe.

MSBuild Project is a nice introduction to writing and understanding project files.

To build our demo program we can use a project file named build.csproj with the following content:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="hw.cs" />
  </ItemGroup>
  <Target Name="Compile">
    <Csc Sources="@(Compile)" />
  </Target>
</Project>

To build our program we run MSBuild.exe with the project file as argument:

MSBuild.exe build.csproj

The project defines a DefaultTargets attribute which tells MSBuild that our default Target section is the one named Compile.

This section makes use of the special target Csc which tells MSBuild to compile the list of source files. By giving @(Compile) as value to the Sources attribute we tell MSBuild to use all files defined by the Compile element.

This is really to only begin scratching at the surface of what MSBuild can do but it's a starting point.

Using Visual Studio

There are tons of information, guides and how tos on the net so I will not bother to repeat it here. Hello World in C# is probably a fine starting point.

Resources

  1. Welcome to Visual Studio 2013
  2. Command-line Building With csc.exe
  3. MSBuild
  4. Microsoft Build Tools 2013
  5. Make (software)
  6. Integrated development environment
  7. Microsoft .NET Framework 4.5
  8. Hello World -- Your First Program (C# Programming Guide)
  9. Walkthrough: Creating an MSBuild Project File from Scratch
  10. C# Compiler Options