MSBuild.exe 不接受 /p:DefineConstants 和 /p:PreprocessorDefinitions
我已经阅读了 Stack Overflow 上的很多文章,这些文章回答了“如何从 MSBuild 命令行将预处理器定义传递给编译器”这一问题,并且它们都以以下一些变化进行了回应:
MSBuild.exe /p:DefineConstants=THING_TO_BE_DEFINED
我已经尝试了我所尝试的每一种变化可以想出:
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:
#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 年看到了这一点)在其内部深处设置了一些默认设置,阻止了我的命令行参数的应用,但我没有发现任何证据来支持这个假设。
关于如何让它发挥作用有什么想法吗?为什么以 FSM 的名义他们不坚持使用好的 ol'-D THING_TO_BE_DEFINED 呢?
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"
...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;
}
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果在命令行上调用 MSBuild,则无法指定 DefineConstants 的值。但如果您正在构建 .csproj 或另一个 MSBuild 脚本,则可以指定它。如果您创建一个 msbuild 文件来“替换”您的解决方案文件,那么您可以在构建项目时使用该文件并为其指定值。例如:
然后您可以使用
msbuild.exe buid.proj /p:DefineConstants="YourValue;Debug;Trace"
注意命令行中引号的用法。
我不久前在 http://sedodream 上写了一篇关于与此相关的内容的博客文章。 com/2008/05/07/MSBuildBuildingTheSameProjectMultipleTimes.aspx。
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:
Then you can use
msbuild.exe buid.proj /p:DefineConstants="YourValue;Debug;Trace"
Note the usage of the quotes on the command line.
I have written a blog post a while back about something related to this at http://sedodream.com/2008/05/07/MSBuildBuildingTheSameProjectMultipleTimes.aspx.
如果你想定义TRACE & DEBUG 常量这应该有效:
If you want to define TRACE & DEBUG Constants this should work:
为了使 /p 工作,需要对 vcxproj 进行以下修改。
放
<定义常量>< /定义常量>
在 <PropertyGroup Label=Globals > 下
<PreprocessorDefinitions>$(DefineConstants);WIN32;_DEBUG;_CONSOLE;UNIT_TEST_SIM;%(PreprocessorDefinitions)
这样 MSBuild 就会知道,对于预处理器,它需要使用来自全局 PropertyGroup 的 DefineConstants 的值,除非通过 /p:DefineConstants="MY_DEFINE" 从命令行提供
The below are needed modification to the vcxproj for the /p to work.
put
<DefineConstants>< /DefineConstants>
under the <PropertyGroup Label=Globals >
<PreprocessorDefinitions>$(DefineConstants);WIN32;_DEBUG;_CONSOLE;UNIT_TEST_SIM;%(PreprocessorDefinitions)
This way MSBuild will know that for the preprocessor it needs to use the values from the DefineConstants which come from the Globals PropertyGroup unless provided from the command line by the /p:DefineConstants="MY_DEFINE"
为了完整起见,当我想要
THING_TO_BE_DEFINED="VALUE WANTED"
(用于 VB.NET)和 msbuild 版本 3.5.30729.1(在批处理文件中)时,我发现这是有效的:(全部在一个 批处理文件中)当然行)
For completeness, this is what I found worked when I wanted
THING_TO_BE_DEFINED="VALUE WANTED"
, for VB.NET, and msbuild version 3.5.30729.1, in a batch file:(all on one line of course)