【问题标题】:Getting linker settings in post build script在构建后脚本中获取链接器设置
【发布时间】:2016-10-28 07:02:18
【问题描述】:

我正在尝试编写一个构建后脚本,该脚本应该将所有必需的 dll 复制到 $(OutDir) 目录中。因此脚本必须知道AdditionalLibraryDirectoriesAdditionalDependencies 的列表才能执行复制过程。

有人知道如何在构建后事件脚本中访问它们吗?

【问题讨论】:

  • 将自定义 msbuild 目标添加到项目文件可能更容易,它的语法通常可以更容易地复制这样的东西,尤其是与批处理文件相比。然后,您可以引用诸如 @(Link->'%(AdditionalDependencies )') 之类的库
  • @stijn 这听起来很有趣。你能提供更多细节吗?
  • @stijn 你能通过自定义 msbuild 任务遍历这些列表中的项目吗?
  • 是的,尽管我认为自定义 Target 也有可能。我看看能不能给出答案。

标签: visual-c++ visual-studio-2015 msbuild post-build-event


【解决方案1】:

应该是这样的开始,将其添加到项目文件中<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets"/> 行之后的某处:

<Target Name="GetFullLibPaths" AfterTargets="Build">
  <ItemGroup>
    <Libs Include="%(Link.AdditionalDependencies)"/>
    <LibDirs Include="%(Link.AdditionalLibraryDirectories)"/>
    <!-- Combine them, 'cross product' -->
    <LibsAndDirs Include="@(Libs)">
      <Dir>%(LibDirs.Identity)</Dir>
    </LibsAndDirs>
    <!-- Filter on those which actually exist -->
    <LibPaths Include="@(LibsAndDirs)" Condition="Exists('%(LibsAndDirs.Dir)%(LibsAndDirs.Identity)')">
      <FullLibPath>%(Dir)\%(Identity)</FullLibPath>
    </LibPaths>
  </ItemGroup>

  <Message Text="MatchingLibs = %(LibPaths.FullLibPath)" />
</Target>

它会在构建后自动运行,列出库和目录,并寻找有效存在于库目录中的库(跳过标准的 user32.lib 等)。

然后您可以将这些 dll 复制到 OutDir,假设这些 dll 也在库目录中:

<Copy SourceFiles="%(LibPaths.Dir)\%(LibPaths.Filename).dll" DestinationFiles="$(OutDir)"/>

【讨论】:

  • 在项目 vcyproj 中插入上层代码 sn-p 后,编译此项目时 Visual Studio 的输出窗口中没有消息。
  • 这很奇怪,甚至MatchingLibs = 都不是?你在哪里插入代码?项目是否构建(例如,如果它已经是最新的,VS 甚至不会开始构建)?
猜你喜欢
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
相关资源
最近更新 更多