l33t
l33t

Reputation: 19966

Define C# preprocessor from MsBuild

In my C# file I want to have a preprocessor condition like this:

#if DEMO
    ShowSplash();
#endif

I'm running this command from command line:

MSBuild MySolution.sln /p:Configuration=Release /p:Platform="Any CPU" /p:DEMO=1

Then, in MyProject.csproj file I have the following:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DefineConstants>TRACE;DEMO=$(DEMO)</DefineConstants>
</PropertyGroup>

But the preprocessor seems to skip my splash code. (I'm aware of the difference between "Any CPU" and "AnyCPU". I never touched that, so I'm quite sure Visual Studio doesn't care about the space.)

DEMO is not defined? The same construct seems to work in other project types (e.g. .wixproj) What am I missing here?

Upvotes: 3

Views: 5960

Answers (1)

JM Lauer
JM Lauer

Reputation: 232

First, you should only define (and test in your code) the symbol, here: DEMO

Then, you should conditionally add your symbol to existing symbols (those eventualy defined in project's properties):

In .csproj file, after first item <DefineConstants> or creating another <PropertyGroup> section, add line:

<DefineConstants Condition="'$(DEMO)'=='1'">$(DefineConstants);DEMO</DefineConstants>

PS: this is a tested solution.

Upvotes: 9

Related Questions