C# 使用 MSBuild 从命令行定义预处理器值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/485237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:15:43  来源:igfitidea点击:

Define a preprocessor value from command line using MSBuild

c#compiler-constructionmsbuildc-preprocessor

提问by BrianH

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 构建我们的应用程序,我最好使用以下内容:

MSBuild -DefineProperty:DEMO MySolution.sln

Does such functionality exist?

是否存在这样的功能?

采纳答案by Michael Stum

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.

这是这个的副本,是的, /p:DefineConstants 确实可以正常工作,并且配置器是正确的,这将覆盖项目文件中已经定义的所有条件符号(恕我直言,这很好),因此您必须定义它们全部。

回答by configurator

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.

您必须包含 DEBUG 或 RELEASE 以及已在解决方案文件中定义的任何其他常量,但我认为这应该可行。免责声明:我自己从未真正尝试过。

回答by Glazius

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.

MSBuild 中的 /p 指令不限于构建文件中已存在的属性。你可以用它来设置任何东西。

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

因此,例如,如果您使用 $(FeatureSet) 引导预处理器指令,然后调用 MSBuild,例如

MSBuild solution.sln /p:FeatureSet=DEMO

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

它相应地获得#defined,而无需手动破坏并重新指定您正在运行的任何其他预处理器指令。

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.

我已经验证了这在 VS2010 中有效。不太确定如何为没有 MSBuild 的 Visual Studio 内部完成的构建定义 FeatureSet。