Tech Dreams

How To Define Conditional Compilation Symbols In MSBuild Automated C# Compilations [Programming]

In one of the mobile device application we are developing using .NET Compact Framework(C#) utilizes conditional compilation to include/exclude device specific source code. In Visual Studio, we can define the conditional compilation constants using “Conditional compilation symbols” input box in Build tab of Project Properties.

visual_studio_specifying_conditional_compilation_symbols

MSBuild and Conditional Parameters

Everything was working as expected until we checked in the source code and forced an automated build that makes use of MSBuild.exe to compile code. The problem is MSBuild not picking up the Conditional compilation symbols defined using Visual Studio properties page, so the code was not compiling as expected.

To resolve the issue we passed the required conditional compilation symbols to MSBuild command as extra parameters. Here is the extra parameters we specified to the command using /p":DefineConstants switch

 

              /p:DefineConstants="PocketPC,DEVICE"

 

Few points to note

  • Make sure that symbols are enclosed between double quotes, when passed as parameters. For example you should not pass the switch as /p:DefineConstants=PocketPC,DEVICE
  • If there are specifying more than one symbol then separate them using comma but not with semicolon(as few people are reporting problems with semicolon). We tested with comma separation and it worked perfectly.