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.

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

  1. Patrick
    October 5, 2010 at 12:10 pm (8 years ago)

    Hi, i also tried the Conditional Parameters, but when i use the command line to use a special parameter like DEMO or LIVE it always jumps in LIVE:

    #if (LIVE)
    Console.WriteLine(“LIVE”);
    #else
    Console.WriteLine(“DEMO”);
    #endif

    /p:DefineConstants=”LIVE” for Live execution

    /p:DefineConstants=”DEMO” for Demo execution

    whats wrong?

    Reply

Leave a reply