Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define C# preprocessor from MsBuild

Tags:

c#

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?

like image 300
l33t Avatar asked Sep 16 '25 19:09

l33t


1 Answers

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.

like image 92
JM Lauer Avatar answered Sep 18 '25 08:09

JM Lauer