Sni*_*gus 20 msbuild macros command-line c-preprocessor
我已经阅读了很多关于Stack Overflow的文章,这些文章回答了"如何从MSBuild命令行向编译器传递预处理器定义"的问题,并且他们都回复了一些变体:
MSBuild.exe /p:DefineConstants=THING_TO_BE_DEFINED
Run Code Online (Sandbox Code Playgroud)
我已经尝试了我能提出的每一个变体:
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"
Run Code Online (Sandbox Code Playgroud)
......还有其他几十个人.我也以类似的方式调整了重写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;
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用上面的简单命令行应用程序,以及我在这里的一个巨大的游戏项目.我只能猜测Visual Studio(我在2005年和2008年看到这个)有一些默认设置深入阻止我的命令行参数被应用,但我没有找到证据来支持这个假设.
关于如何让这个工作的任何想法?为什么以FSM的名义并没有坚持使用好的'-D THING_TO_BE_DEFINED?
如果在命令行上调用MSBuild,则无法指定DefineConstants的值.但是,如果要构建.csproj或其他MSBuild脚本,则可以指定它.如果您创建一个msbuild文件来"替换"您的解决方案文件,那么您可以使用它来在构建项目时指定该值.例如:
<?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>
Run Code Online (Sandbox Code Playgroud)
然后你可以使用 msbuild.exe buid.proj /p:DefineConstants="YourValue;Debug;Trace"
请注意命令行中引号的用法.
我在http://sedodream.com/2008/05/07/MSBuildBuildingTheSameProjectMultipleTimes.aspx上写了一篇关于此相关内容的博客文章.
如果你想定义TRACE和DEBUG常量,这应该工作:
msbuild mysln.sln /t:Rebuild /p:Configuration=Release /p:DefineConstants="DEBUG;TRACE"
Run Code Online (Sandbox Code Playgroud)
为了使 /p 工作,需要对 vcxproj 进行以下修改。
put < DefineConstants ></ DefineConstants >
在 < PropertyGroup Label=Globals > 下
<预处理器定义>$(定义常量);WIN32;_DEBUG;_CONSOLE;UNIT_TEST_SIM;%(预处理器定义)
这样 MSBuild 就会知道,对于预处理器,它需要使用来自 Globals PropertyGroup 的 DefineConstants 的值,除非 /p:DefineConstants="MY_DEFINE" 从命令行提供