Define a preprocessor value from command line using MSBuild [duplicate]

烂漫一生 提交于 2019-12-22 01:27:09

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/485237/define-a-preprocessor-value-from-command-line-using-msbuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!