【发布时间】:2015-02-19 11:12:28
【问题描述】:
我正在尝试为我的 NuGet 包创建一个 .targets 文件,该文件将链接到一个正确的 .lib 文件,具体取决于项目的 C++ 运行时库。 This answer 建议为此使用 %(ClCompile.RuntimeLibrary) 元数据。但似乎无法在<Target> 节点之外访问元数据!并且库依赖添加在<ItemDefinitionGroup> 节点中,就在根<Project> 节点下。
这里是 SSCCE:
<?xml version="1.0" encoding="us-ascii"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="main.cpp">
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
</ClCompile>
</ItemGroup>
<PropertyGroup>
<RuntimeLibrary>%(ClCompile.RuntimeLibrary)</RuntimeLibrary>
</PropertyGroup>
<Target Name="Build">
<Message Text="Property = $(RuntimeLibrary)" Importance="high" />
<Message Text="Metadata = %(ClCompile.RuntimeLibrary)" Importance="high" />
</Target>
</Project>
使用 MsBuild 运行它会产生:
Property = %(ClCompile.RuntimeLibrary)
Metadata = MultiThreadedDebugDLL
同样的语句%(ClCompile.RuntimeLibrary)在<Target>节点内使用时扩展为值,但在<Target>节点外的<PropertyGroup>节点中使用时不会扩展为值。
那么如何访问运行时库元数据值以添加引用正确的库?
更新:建议但不令人满意的解决方法是定义RuntimeLibrary,如下所示:
<RuntimeLibrary>@(ClCompile->'%(RuntimeLibrary)')</RuntimeLibrary>
在这种情况下,初始脚本的输出是正确的,但是我的任务仍然没有解决,因为我想在一个条件下使用这个属性。因此,如果我添加以下内容:
<PropertyGroup Condition="'$(RuntimeLibrary)'=='MultiThreadedDebugDLL'">
<TestProp>defined</TestProp>
</PropertyGroup>
...
<Message Text="TestProp = $(TestProp)" Importance="high" />
TestProp 未定义。如何使这项工作适应条件?
【问题讨论】:
-
re:您的编辑:如果您将该 PropertyGroup 放入 Target 本身,或者如果您使用
Condition="'@(ClCompile->'%(RuntimeLibrary)')',这将起作用。我不是 100% 确定,但我认为由于 msbuild 使用的评估顺序,您编写它的方式将行不通。 -
@stijn 我不能把这个属性放在
<Target>中,因为我需要在<ItemDefinitionGroup>的条件下使用它,它不能放在<Target>中:( -
那么
@的条件似乎是唯一的解决方案? -
@stijn 是的,但它也不起作用。此外,
@(ClCompile->'%(RuntimeLibrary)')扩展为类似MultiThreadedDebugDLL;MultiThreadedDebugDLL;MultiThreadedDebugDLL的字符串,以防项目中有多个文件(几乎总是如此),并且需要在使用前进行拆分。不确定是否可以在条件本身中进行这种拆分。
标签: c++ visual-studio msbuild nuget msvcrt