子项目的 CMake 覆盖策略

CMake override policy for subproject

我的 CMake 项目编译了一个 Python .so/.dylib 扩展模块链接到一个大的静态库。我想编译子项目的静态库,隐藏符号的可见性:它允许链接器(或 LTO 优化器)丢弃我的 python 模块未使用的符号。

最好的方法是在子项目的静态库目标上使用 CXX_VISIBILITY_PRESET 和朋友。 但是,子项目通过声明使用 3.1.0 中的策略:

cmake_minimum_required (VERSION 3.1.0)

策略 CMP0063 NEW: Honor visibility properties for all target types. 仅从版本 3.3 引入,因此 set_target_properties 没有任何效果。

我的项目需要CMake 3.3,但我无法控制子项目。

我想避免修补子项目 CMakeLists.txt,但目前我看不到其他方法。

有什么想法吗?

您可以使用 add_compile_option 指定适当的编译器选项。

cmake_minimum_required() 对 CMake 策略有以下影响:

  • All policies introduced in the specified version or earlier will be set to use NEW behavior.
  • All policies introduced after the specified version will be unset.

但是你可以使用CMake的CMAKE_POLICY_DEFAULT_CMP<NNNN>全局变量来"default for CMake Policy CMP when it is otherwise left unset."

这是一个例子:

set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
add_subdirectory(MySubProjDir)

参考