CMake target_link_directories正在添加额外的意外目录,我怎么能删除它们?

wnavrhmk  于 2023-06-29  发布在  其他
关注(0)|答案(1)|浏览(158)

考虑以下几点:

target_link_directories(${PROJECT_NAME} PUBLIC
    "../MyProjectNameSharedComponents/glew/lib/x64"
    "../MyProjectNameSharedComponents/OpenImageIO/Windows/lib"
#and etc.

这将导致解决方案中的“其他库目录”中出现以下行:

C:/Users/user/Documents/gpu-open/MyProjectName/MyProjectName.Src/../MyProjectNameSharedComponents/glew/lib/x64
C:/Users/user/Documents/gpu-open/MyProjectName/MyProjectName.Src/../MyProjectNameSharedComponents/glew/lib/x64/$(Configuration)
C:/Users/user/Documents/gpu-open/MyProjectName/MyProjectName.Src/../MyProjectNameSharedComponents/OpenImageIO/Windows/lib
C:/Users/user/Documents/gpu-open/MyProjectName/MyProjectName.Src/../MyProjectNameSharedComponents/OpenImageIO/Windows/lib/$(Configuration)

通过生成器表达式生成的行显示类似的行为,例如

"$<$<CONFIG:Debug2022>:"
    "$ENV{MAYA_X64_2022}/lib"
">"

生产:

C:/Program Files/Autodesk/Maya2022/lib
C:/Program Files/Autodesk/Maya2022/lib/$(Configuration)

在macOS上情况更糟,CMake将$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)添加到Xcode项目的LibrarySearchPath行中。
这行“/$(Configuration)”是从哪里来的,我如何删除它?

xiozqbni

xiozqbni1#

在撰写本文时(CMake 3.27是最新版本),对于Visual Studio生成器,看起来这是硬编码在CMake源代码中,没有任何方法禁用它。
cmVisualStudio10TargetGenerator.cxxcmVisualStudio10TargetGenerator::ComputeLinkOptions

std::vector<std::string> const& ldirs = cli.GetDirectories();
  std::vector<std::string> linkDirs;
  for (std::string const& d : ldirs) {
    // first just full path
    linkDirs.push_back(d);
    // next path with configuration type Debug, Release, etc
    linkDirs.push_back(d + "/$(Configuration)");
  }

您可以使用build your own modified version of CMakeask the maintainers to add a feature来切换此行为。
对于Xcode生成器,看起来你可以通过改变policy CMP0142cmake_policy(SET CMP0142 NEW))来禁用它。引用文档:

  • 新版本3.25*。

Xcode生成器不会将per-config后缀附加到库搜索路径。
在CMake 3.24及更低版本中,Xcode生成器在库搜索路径的每个条目之前添加一个自身的副本,并附加$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)。这是早期版本的CMake遗留下来的,在早期版本中,每个配置的目录都没有很好地建模。这样的路径通常不存在,导致来自工具链的警告。CMake 3.25及更高版本不希望添加此类库搜索路径。此策略为可能意外依赖旧行为的项目提供了兼容性。
此策略的OLD行为是将$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)附加到所有库搜索路径。NEW行为是不修改库搜索路径。
该策略在CMake 3.25版本中引入。使用cmake_policy()命令将其显式设置为OLDNEW。与许多策略不同,CMake版本3.27.0-rc 3在未设置此策略时不会发出警告,而只是使用OLD行为。
参见cmGlobalXCodeGenerator.cxxcmGlobalXCodeGenerator::AddDependAndLinkInformation

// add the library search paths
    {
      BuildObjectListOrString libSearchPaths(this, true);

      std::string linkDirs;
      for (auto const& libDir : cli->GetDirectories()) {
        if (!libDir.empty() && libDir != "/usr/lib") {
          cmPolicies::PolicyStatus cmp0142 =
            target->GetTarget()->GetPolicyStatusCMP0142();
          if (cmp0142 == cmPolicies::OLD || cmp0142 == cmPolicies::WARN) {
            libSearchPaths.Add(this->XCodeEscapePath(
              libDir + "/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)"));
          }
          libSearchPaths.Add(this->XCodeEscapePath(libDir));
        }
      }

你需要问维护人员为什么这些东西会是这样的(至少从历史的Angular 来看)。参见https://discourse.cmake.org/。如果你问,请在这里发表评论,并链接到话语页面。

相关问题