MSBuild.exe 不接受/p:DefineConstants 或/p:PreprocessorDefinitions [英] MSBuild.exe not accepting either /p:DefineConstants nor /p:PreprocessorDefinitions

查看:17
本文介绍了MSBuild.exe 不接受/p:DefineConstants 或/p:PreprocessorDefinitions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于 Stack Overflow 的文章,这些文章回答了如何从 MSBuild 命令行将预处理器定义传递给编译器"这个问题,它们都给出了以下一些变化:

I've been through quite a number of articles on Stack Overflow that answered the question "How do I pass preprocessor definitions to the compiler from the MSBuild command line," and they all responded with some variation of:

MSBuild.exe /p:DefineConstants=THING_TO_BE_DEFINED

我已经尝试了所有我能想到的变化:

I have tried every variation that I could come up with:

MSBuild.exe "/p:DefineConstants=THING_TO_BE_DEFINED"
MSBuild.exe /p:DefineConstants="THING_TO_BE_DEFINED"
MSBuild.exe "/p:DefineConstants=THING_TO_BE_DEFINED=1"
MSBuild.exe /p:DefineConstants="THING_TO_BE_DEFINED=1"

...和其他几十个.我也曾以类似的方式尝试过重写 PreprocessorDefinitions.他们都触发了下面的#error:

...and dozens of others. I've also flirted with overriding PreprocessorDefinitions in similar ways. All of them triggered the #error below:

#include "stdafx.h"

#if !defined(THING_TO_BE_DEFINED)
#error "THING_TO_BE_DEFINED is not defined"
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

我一直在尝试使用上面的简单命令行应用程序以及我在这里拥有的一个大型游戏项目.我只能猜测 Visual Studio(我在 2005 年和 2008 年看到这一点)在其内部有一些默认设置,这阻止了我的命令行参数被应用,但我没有找到支持这个假设的证据.

I've been trying this with the simple command-line application above, as well as with a huge game project that I have here. I can only guess that Visual Studio (I'm seeing this with 2005 and 2008) has some default set deep in its bowels that is preventing my command line argument from being applied, but I've found no evidence to support this hypothesis.

关于如何让它发挥作用的任何想法?为什么他们不以 FSM 的名义坚持良好的 ol' -D THING_TO_BE_DEFINED?

Any ideas on how I can get this to work? Why in the name of FSM didn't they stick with good ol' -D THING_TO_BE_DEFINED?

推荐答案

如果您在命令行上调用 MSBuild,则无法指定 DefineConstants 的值.但如果您正在构建 .csproj 或其他 MSBuild 脚本,则可以指定它.如果您创建一个 msbuild 文件来替换"您的解决方案文件,那么您可以在构建项目时使用它来指定该文件的值.例如:

If you are calling MSBuild on the command line you cannot specify the value for DefineConstants. But if you are building a .csproj, or another MSBuild script, then you can specify it. If you create a msbuild file to "replace" your solution file then you can use that an specify the value for that when you build your projects. For example:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <!-- Default value here -->
    <DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
  </PropertyGroup>

  <ItemGroup>
    <Projects Include="one.csproj" />
    <Projects Include="two.csproj" />
  </ItemGroup>

  <Target Name="Build">
    <MSBuild Projects="@(Projects)"
                 Properties="DefineConstants=$(DefineConstants)"/>
  </Target>
</Project>

然后你可以使用 msbuild.exe buid.proj/p:DefineConstants="YourValue;Debug;Trace"

注意命令行中引号的用法.

Note the usage of the quotes on the command line.

不久前,我在 http:///sedodream.com/2008/05/07/MSBuildBuildingTheSameProjectMultipleTimes.aspx.

这篇关于MSBuild.exe 不接受/p:DefineConstants 或/p:PreprocessorDefinitions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆