C# 如何将条件编译符号 (DefineConstants) 传递给 msbuild
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10714668/
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
How do you pass conditional compilation symbols (DefineConstants) to msbuild
提问by Steven P
So, both thisand thisare pretty clear. Simply pass /p:DefineConstants="SYMBOL"
所以,这个和这个都很清楚。简单通过/p:DefineConstants="SYMBOL"
It doesn't work at all for me, even in a test project. I'm expecting that passing /p:DefineConstants="SYMBOL" will override any conditional compilation constants defined in the csproj. Not the case however...
它对我来说根本不起作用,即使在测试项目中也是如此。我期望传递 /p:DefineConstants="SYMBOL" 将覆盖 csproj 中定义的任何条件编译常量。然而事实并非如此……
Full code listing below:
完整代码清单如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefineConstants
{
class Program
{
static void Main(string[] args)
{
#if DEV
Console.WriteLine("DEV");
#elif UAT
Console.WriteLine("UAT");
#else
Console.WriteLine("No environment provided");
#endif
}
}
}
.csproj file is:
.csproj 文件是:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{57A2E870-0547-475C-B0EB-66CF9A2FE417}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DefineConstants</RootNamespace>
<AssemblyName>DefineConstants</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
built using:
使用:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild DefineConstants.sln /p:DefineConstants="DEV;DEBUG" /p:Configuration="Debug" /p:Platform="x86"
Running the program shows:
运行程序显示:
No environment provided
Help!
帮助!
采纳答案by stijn
Using DefineConstantsdefinitely does work. Which means you are doing something wrong. My guess is you first built the project without defining anything, then built it again. MSBuild will then see the project was built already and won't build again, but just copy output files. You should post the output of msbuild so we can be sure, but for reference I compiled your project using the necessary switches only and here is the result (full msbuild output omitted):
使用DefineConstants肯定有效。这意味着你做错了什么。我的猜测是您首先在没有定义任何内容的情况下构建了项目,然后再次构建了它。然后 MSBuild 将看到项目已经构建并且不会再次构建,而只是复制输出文件。您应该发布 msbuild 的输出,以便我们可以确定,但作为参考,我仅使用必要的开关编译了您的项目,结果如下(省略了完整的 msbuild 输出):
> msbuild ConsoleApplication1.sln /p:DefineConstants="DEV" /t:Rebuild
....
Building solution configuration "Debug|x86".
Project ... is building ConsoleApplication1.csproj" (Rebuild target(s)).
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
DEV
> msbuild ConsoleApplication1.sln /p:DefineConstants="UAT" /t:Rebuild
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
UAT
> msbuild ConsoleApplication1.sln /t:Rebuild
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
No environment provided
回答by Sanket Raval
It seems you are not passing conditional compilation symbol. That's why you are getting output as : No environment provided. Just go to project properties and than click on build tab (left side below application). you will find one box asking for conditional compilation symbol. Just write your constants as per your desired output. for ex. in your example, to print "DEV" write DEV at conditional compilation box and rebuild your project. i am sure it will work.
看来您没有传递条件编译符号。这就是为什么你得到的输出是:没有提供环境。只需转到项目属性,然后单击构建选项卡(应用程序下方的左侧)。你会发现一个要求条件编译符号的框。只需根据您想要的输出编写您的常量。例如。在您的示例中,要打印“DEV”,请在条件编译框中写入 DEV 并重建您的项目。我相信它会起作用。
Note: if you want pass any symbol than as per your code it will print : No environment selected.
注意:如果您想传递任何符号而不是根据您的代码,它将打印:未选择环境。

