3

I have a project with a preprocessor constant defined for a configuration

<DefineConstants>TRACE;DEBUG;VAR1</DefineConstants>

My code could be :

#if VAR1
        Console.WriteLine("Var1");
#endif
#if VAR2
        Console.WriteLine("Var2");
#endif

I want execute MSBuild and define the constant VAR2 without override constants defined in the configuration

I try :

MSBuild MyProject.csproj /p:DefineConstants=VAR2

But constants defined in the project file are not set : my program displays only

Var2

I have tried things like without success :

MSBuild MyProject.csproj /p:DefineConstants=$(DefineConstants);VAR2

or

<DefineConstants>$(DefineConstants);TRACE;DEBUG;VAR1</DefineConstants>

Is it a way to merge the constants defined in the project and constants defined on the command line?

Troopers
  • 5,127
  • 1
  • 37
  • 64

1 Answers1

8

AFAIK a property defined via commandline will always override a statically defined property with the same name. As a workaround you can enqueue an additional property to <DefineConstants /> that is not defined in your project file:

<DefineConstants>TRACE;DEBUG;VAR1;$(MyCommandLineConstants)</DefineConstants>

and call MSBuild this way:

MSBuild MyProject.csproj /p:MyCommandLineConstants=VAR2;VAR3
mkko
  • 332
  • 2
  • 7