4

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?

5
  • Did you try clean/rebuild the project after editing .csproj? Commented May 7, 2017 at 20:53
  • No, but I do see this now: warning MSB3052: The parameter to the compiler is invalid, '/define:DEMO=1' Commented May 7, 2017 at 20:58
  • 3
    <DefineConstants>TRACE</DefineConstants> <DefineConstants Condition=" $(DEMO) == 1 ">$(DefineConstants);DEMO</DefineConstants> Commented May 7, 2017 at 21:32
  • You could perhaps use the command line somewhat differently, like MSBuild MySolution.sln /p:Configuration=Release /p:Platform="Any CPU" /p:Conditionals=DEMO. In your project you would then use <DefineConstants>TRACE;$(Conditionals)</DefineConstants>. (might or might not work; i have not tested it...) Commented May 7, 2017 at 22:06
  • @PetSerAl, that works. It seems that CSC does not support #if X=1. Put that stuff in an answer and I'll accept. Commented May 7, 2017 at 22:09

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.