Alex Rivera | Logout

Define a preprocessor value from command line using MSBuild

Asked 2009-01-27T21:01:53.630
25

I need to create a demo version of an existing large application consisting of multiple projects. I'd like to use the existing projects, and just neuter the functionality via preprocessor directives

#if DEMO
    mycode.NeuterNow();
#endif

We are building our app using MSBuild, and I'd ideally use something along the lines of:

MSBuild -DefineProperty:DEMO MySolution.sln

Does such functionality exist?

Edit
Report

1 Answer

8

I discovered something interesting when pursuing my own solution to this problem and I thought I'd share.

The /p directive in MSBuild isn't limited to properties that already exist in a build file. You can use it to set anything.

So if, for example, you lead your preprocessor directives with $(FeatureSet) and then call MSBuild as, for example

MSBuild solution.sln /p:FeatureSet=DEMO

it gets #defined accordingly without having to manually clobber and respecify any other preprocessor directives you have running.

I've verified this works in VS2010. Not quite as sure about how you'd define FeatureSet for a build done inside Visual Studio without MSBuild.

answered 2012-12-28T08:03:16.580

Your Answer