我正在尝试创建一个自定义的MSBuild目标来自动获取SVN版本号到某个定义中,比如MY_VERSION.因此,我的目标文件(get-svn-info.targets):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="GetSvnRevision" BeforeTargets="PrepareForBuild">
<Exec Command="svn info --show-item revision" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="SvnRevisionNum" />
</Exec>
<ItemGroup>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);MY_VERSION=$(SvnRevisionNum);</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
</ItemGroup>
<Message Text="MY_VERSION=$(SvnRevisionNum)" Importance="high"></Message>
</Target>
</Project>
最后,修改了项目文件:
<Project ...
<Import Project="get-svn-info.targets" />
...
</Project>
但编译器显示“MY_VERSION”:此处未声明标识符
std::cout << "Hello World!\n" << std::to_string(MY_VERSION) << std::endl;
我做错了什么?为什么PreprocessorDefinitions仍然具有旧值?谢谢
构建日志输出(跳过许多不重要的行):
...
1>Target "GetSvnRevision" in file "D:\ConsoleApplication1\get-svn-info.targets":
1> Using "Exec" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1> Task "Exec"
1> Task Parameter:ConsoleToMSBuild=True
1> Task Parameter:Command=svn info --show-item revision
1> svn info --show-item revision
1> 30
1> Output Property: SvnRevisionNum=30
1> Done executing task "Exec".
1> Task "Message"
1> Task Parameter:Importance=high
1> Task Parameter:Text=MY_VERSION=30
1> MY_VERSION=30
1> Done executing task "Message".
1>Done building target "GetSvnRevision" in project "ConsoleApplication1.vcxproj".
...
1>Target "ClCompile" in file "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets":
1> Task "CL"
...
1> Task Parameter:
1> Sources=
1> ConsoleApplication1.cpp
1> PreprocessorDefinitions=WIN32;_DEBUG;_CONSOLE;_UNICODE;UNICODE;
...
1> Task Parameter:
1> PreprocessorDefinitions=
1> WIN32
1> _DEBUG
1> _CONSOLE
1> _UNICODE
1> UNICODE
...
1> D:\ConsoleApplication1\ConsoleApplication1.cpp(9,53): error C2065: 'MY_VERSION': undeclared identifier
1> The command exited with code 2.
1> Done executing task "CL" -- FAILED.
1>Done building target "ClCompile" in project "ConsoleApplication1.vcxproj" -- FAILED.
1>
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
...
2条答案
按热度按时间uqdfh47h1#
ItemDefinitionGroup
的语法不正确。ItemDefinitionGroup
不应位于ItemGroup
内。ItemDefinitionGroup
将添加已更改的PreprocessorDefinitions
作为ClCompile
ItemGroup中项目的默认值。但是
ItemDefinitionGroup
不能在Target
中使用。但是我们仍然可以修改
ClCompile
ItemGroup中的项目的元数据。我建议设两个目标。
“GetSvnRevision”可以提前执行。但“ApplySvnRevision”应在编译之前执行。
gk7wooem2#
这个解决方案是可行的,但也许有一个更优雅的解决方案?毕竟,MY_VERSION不仅可以在一个文件ConsoleApplication1.cpp中使用?谢谢