CMake 政策的范围是什么?
What is the scope of CMake policies?
我有一个包含多个子目录的 CMake 项目,例如:
dir1
subdir11
subdir12
dir2
subdir21
subdir22
根目录 CMakeLists.txt:
add_subdirectory(dir1)
add_subdirectory(dir2)
dir1 和 dir2 中的 CMakeLists 相似:
add_subdirectory(subdir11)
add_subdirectory(subdir12)
和
add_subdirectory(subdir21)
add_subdirectory(subdir22)
子目录中的 CMakeLists 做实际工作。
文件 dir1/subdir12/CMakeLists.txt
将 CMP0046
策略设置为 OLD
:
cmake_policy(SET CMP0046 OLD) #silently ignore missing dependencies
我的问题是 - CMP0046 的这个设置会传播到 subdir21
和 subdir22
吗?
没有。这个问题最好直接回答 from the documentation...
Policy settings are scoped using a stack. A new level of the stack is
pushed when entering a new subdirectory of the project (with
add_subdirectory) and popped when leaving it. Therefore setting a
policy in one directory of a project will not affect parent or sibling
directories but will affect subdirectories.
要对特定级别进行临时更改,而不包括 sub_directories,您可以使用
cmake_policy(PUSH)
cmake_policy(POP)
如果您希望在 subdir21 和 subdir22 中应用该策略,您需要明确地将其添加到那里或考虑将其添加到共同的父级。
根据此答案的评论 https://unix.stackexchange.com/a/512695/48776
可以使用 set(CMAKE_POLICY_DEFAULT_CMP0046 OLD)
全局设置策略
我试过不同的 3.x 版本,都有效。
我有一个包含多个子目录的 CMake 项目,例如:
dir1
subdir11
subdir12
dir2
subdir21
subdir22
根目录 CMakeLists.txt:
add_subdirectory(dir1)
add_subdirectory(dir2)
dir1 和 dir2 中的 CMakeLists 相似:
add_subdirectory(subdir11)
add_subdirectory(subdir12)
和
add_subdirectory(subdir21)
add_subdirectory(subdir22)
子目录中的 CMakeLists 做实际工作。
文件 dir1/subdir12/CMakeLists.txt
将 CMP0046
策略设置为 OLD
:
cmake_policy(SET CMP0046 OLD) #silently ignore missing dependencies
我的问题是 - CMP0046 的这个设置会传播到 subdir21
和 subdir22
吗?
没有。这个问题最好直接回答 from the documentation...
Policy settings are scoped using a stack. A new level of the stack is pushed when entering a new subdirectory of the project (with add_subdirectory) and popped when leaving it. Therefore setting a policy in one directory of a project will not affect parent or sibling directories but will affect subdirectories.
要对特定级别进行临时更改,而不包括 sub_directories,您可以使用
cmake_policy(PUSH)
cmake_policy(POP)
如果您希望在 subdir21 和 subdir22 中应用该策略,您需要明确地将其添加到那里或考虑将其添加到共同的父级。
根据此答案的评论 https://unix.stackexchange.com/a/512695/48776
可以使用 set(CMAKE_POLICY_DEFAULT_CMP0046 OLD)
我试过不同的 3.x 版本,都有效。