链接来自另一个项目的所有 .obj 文件

mak*_*umi 5 msbuild visual-studio visual-c++ visual-studio-2012

我有一个用于包含嵌入式单元测试的可执行 C++ 项目。我已将测试移至另一个 (.dll) 项目,以便将它们与 VS Test Runner 一起使用,除了一堆未解决的外部符号错误外,一切都很好。

如果我将 .exe 中的所有 .obj 文件链接到 .dll 中,事情似乎可以正常工作,但是其中有很多,并且对它们进行硬编码非常“脏”,因为列表总是可以更改。

我尝试添加 .exe 项目作为参考并将“使用库依赖项输入”设置为 True,但它似乎没有做任何事情。

mak*_*umi 4

在检查 Microsoft.CppBuild.targets 后,我发现UseLibraryDependencyInputs仅当项目是 StaticLibrary 时,才会包含设置为 true 的项目的 .obj 文件,这对应于在 VS 中检查“使用库依赖项输入”。

解决这个问题的一种快速而肮脏的方法是覆盖ResolvedLinkObjs.vcxproj 中的任务,并稍加修改。

对此进行测试,我能够链接我引用的项目中的所有 .obj 文件。

我将MSBuild 任务输出ItemName的更改为并添加到传递的s. 将 .exe 项目视为调用此目标的副作用并不是立即显而易见的,但我没有注意到任何不好的事情。TargetOutputs"ProjectReferenceToLink"ConfigurationType=StaticLibraryPropertieStaticLibrary

  <Target Name="ResolvedLinkObjs" DependsOnTargets="$(CommonBuildOnlyTargets)">
    <MSBuild Projects="@(_MSBuildProjectReferenceExistent)"
             Targets="GetResolvedLinkObjs"
             BuildInParallel="$(BuildInParallel)"
             Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration); %(_MSBuildProjectReferenceExistent.SetPlatform); ConfigurationType=StaticLibrary"
             Condition="'%(_MSBuildProjectReferenceExistent.Extension)' == '.vcxproj' and '@(ProjectReferenceWithConfiguration)' != '' and '@(_MSBuildProjectReferenceExistent)' != ''"
             ContinueOnError="!$(BuildingProject)"
             RemoveProperties="%(_MSBuildProjectReferenceExistent.GlobalPropertiesToRemove)">
      <Output TaskParameter="TargetOutputs" ItemName="ProjectReferenceToLink" />
    </MSBuild>
  </Target>
Run Code Online (Sandbox Code Playgroud)

这可以轻松修改为仅从一个项目中获取 .obj。更干净的方法也可能涉及不重写像这样的内置目标,而是将其插入到链中。