23

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?

0

3 Answers 3

17

Try

msbuild /p:DefineConstants=DEBUG;DEMO MySolution.sln

You have to include DEBUG or RELEASE and any other constants already defined in the solution file, but I think this should work. Disclaimer: I've never actually tried it myself.

1
  • /p:DefineConstants=DEMO the second ":" needs to be an "="
    – BrianH
    Jan 29, 2009 at 20:19
11

That's a duplicate of this one, and yes, /p:DefineConstants does work fine, and configurator is right, this will override ALL conditional symbols already defined in the Project File (which is good IMHO), so you'll have to define them all.

1
  • 2
    Is there any way to not override?
    – Tsury
    Mar 26, 2015 at 8:17
6

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.

6
  • 2
    Can you give me a full example of this? I can't figure it out how to do this exactly... Sep 10, 2014 at 8:32
  • 1
    Again, this is just VS2010. I don't know about later versions. Preprocessor directives are a semicolon-separated list of items in your project properties, under configuration - C++ - preprocessor. Manually add $(Variable) to the list, then when you call MSBuild from the command line you can set /p:Variable=FOO and things like #ifdef FOO will respond to it.
    – Glazius
    Sep 11, 2014 at 14:54
  • I confirm that it works with VS2017 when /p:DefineConstants wouldn't do it for me. I put $(MyDefines) and used /p:MyDefines="DEMO;NO_OPENCL" when calling MSBuild.exe. Now that I think of it I didn't see anything called "DefineConstants" in my .vcxproj, maybe that's why it didn't work. Nov 15, 2018 at 20:57
  • @MichelRouzic does your solution have 1 project or more than 1? I can't seem to get it to work if there are multiple projects. Rather, I think for some reason VS thinks that the $(Var) is a environment variable which doesn't expand to anything
    – Roy2511
    Sep 3, 2019 at 10:29
  • Only one project, I have no idea how to fix it for more than one. Sep 3, 2019 at 15:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.