.net msbuild,定义条件编译符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/479979/
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-09-03 11:05:28  来源:igfitidea点击:

msbuild, defining Conditional Compilation Symbols

.netmsbuildcompiler-options

提问by Michael Stum

I'm possibly just blind, but is there a command line to specify conditional compilation symbols in MSBUILD?

我可能只是瞎了眼,但是否有命令行可以在 MSBUILD 中指定条件编译符号?

I currently have this Line in my buildscript:

我目前在我的构建脚本中有这一行:

SET MSBUILD=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe
SET CONFIG=Debug
%MSBUILD% /p:Configuration=%CONFIG% /p:OutputPath=..\..\output source\MyProject\MyProject.csproj

And I'd like to add a condition. In Visual Studio, i can just go into Project Properties => Build => Conditional compilation symbols, but I have not seen that option for msbuild?

我想添加一个条件。在 Visual Studio 中,我可以进入项目属性 => 构建 => 条件编译符号,但我还没有看到 msbuild 的那个选项?

Bonus Karma if you know if I can completely override all symbols already specified in the .csproj files to make sure that only the conditionals from my Buildscript go in.

如果你知道我是否可以完全覆盖 .csproj 文件中已经指定的所有符号,以确保只有我的 Buildscript 中的条件进入,那么奖励业力。

回答by Tomalak

Have you seen this? (most info is in the penultimate post)

你见过这个吗?(大多数信息在倒数第二个帖子中)

/p:DefineConstants="MYSYMBOL1;MYSYMBOL2"

回答by Ruben Bartelink

I had to use a space instead of a semicolon a la this post by Bj?rn Lasar: http://www.linqinpark.net/2009/01/13/MSBuildWithMultipleDefineConstants.aspx

在 Bj?rn Lasar 的这篇文章中,我不得不使用空格而不是分号:http: //www.linqinpark.net/2009/01/13/MSBuildWithMultipleDefineConstants.aspx

Update: the blog has disappeared;retrieved via Internet Archive:

更新:博客消失了;通过互联网档案检索

Recently I had to use MSBuild directly to automate some builds. I also had to configure some preprocessor defines based upon a configuration. This is usually done by an Argument like this

最近我不得不直接使用 MSBuild 来自动化一些构建。我还必须根据配置配置一些预处理器定义。这通常由这样的参数完成

"/p:DefineConstants=MY_PREPROC_FLAG"

Nothing special here since there are enough comments on the web about that. Today I needed one Flag more and I used the commandline syntax similar to how I knew it from the IDE:

这里没什么特别的,因为网上有足够多的评论。今天,我还需要一个 Flag,我使用了类似于我从 IDE 中了解到的命令行语法:

"/p:DefineConstants=MY_PREPROC_FLAG;YET_ANOTHER_FLAG"

but this one didn't work.

So the point is that if you want to support multiple defines to a project by commandline you'll have to separate them by simple spaces...

但这个没有用。

所以关键是,如果你想通过命令行支持一个项目的多个定义,你必须用简单的空格将它们分开......

"/p:DefineConstants=MY_PREPROC_FLAG YET_ANOTHER_FLAG" 

and it will be added to the (semicolon-separated) Defines from the IDE. Good to know I think...

它将被添加到 IDE 的(以分号分隔的)定义中。很高兴知道我认为...

回答by leliao

/p:DefineConstantsis an all or nothing deal.

/p:DefineConstants是全有或全无的交易。

If you just want to turn off trace symbol, you can't just do it with: msbuild /p:DefineTrace=false

如果你只想关闭跟踪符号,你不能只用: msbuild /p:DefineTrace=false

You have to define something to override all the symbols already defined: msbuild /p:DefineConstants="RANDOM-SYMBOL"

你必须定义一些东西来覆盖所有已经定义的符号: msbuild /p:DefineConstants="RANDOM-SYMBOL"

Thanks Michael Stum point this hidden rule out I have also wrote a blog about it --- dead link

谢谢 Michael Stum 指出这个隐藏的排除 我也写了一篇关于它的博客 --- 死链接

回答by Matt

What is said in the answers is valid for C#code, and also for ASP.NET "codebehind" C# code. For ASP.NET web projects, if you want to do conditional compilation in the ASPX pages as well, it works a bit differently to conditionally render HTML on the page (note I've removed MasterPageFile="..." AutoEventWireup="true" CodeBehind="..." Inherits="..."which you usually have in the <%@ ... %>declaration as well):

答案中所说的内容适用于C#代码,也适用于 ASP.NET“代码隐藏”C# 代码。对于ASP.NET web 项目,如果您还想在 ASPX 页面中进行条件编译,则在页面上有条件地呈现 HTML 的工作方式略有不同(请注意,我已经删除MasterPageFile="..." AutoEventWireup="true" CodeBehind="..." Inherits="..."了您通常在<%@ ... %>声明中使用的内容):

<%@ Page Title="MyPage" Language="C#" CompilerOptions="/d:DebugSym1;DebugSym2" %>

<% #if DebugSym1 %>         
    <h4>Section1</h4>
<% #else %>
    <h4>(Section 1 skipped)</h4>
<% #endif %>

<% #if DebugSym2 %>         
    <h4>Section2</h4>
<% #else %>
    <h4>(Section 2 skipped)</h4>
<% #endif %>

If you remove DebugSym1or DebugSym2from the CompilerOptions, then the #elsepart of the relevant #ifstatement is rendered.

如果您从 中删除DebugSym1或,则会呈现相关语句的部分。DebugSym2CompilerOptions#else#if

I thought this was worth mentioning for completeness of this topic and can save you time. More you can find in this article, if you're interested.

我认为这对于本主题的完整性来说值得一提,并且可以节省您的时间。如果您有兴趣,可以在本文中找到更多内容。