CMake政策的范围是什么?

wl2*_*776 7 cmake

我有一个带有多个子目录的CMake项目,如下所示:

dir1
    subdir11
    subdir12
dir2
    subdir21
    subdir22
Run Code Online (Sandbox Code Playgroud)

根CMakeLists.txt:

add_subdirectory(dir1)
add_subdirectory(dir2)
Run Code Online (Sandbox Code Playgroud)

dir1和dir2中的CMakeList相似:

add_subdirectory(subdir11)
add_subdirectory(subdir12)
Run Code Online (Sandbox Code Playgroud)

add_subdirectory(subdir21)
add_subdirectory(subdir22)
Run Code Online (Sandbox Code Playgroud)

子目录中的CMakeLists可以实际工作。

该文件dir1/subdir12/CMakeLists.txtCMP0046策略设置为OLD

cmake_policy(SET CMP0046 OLD) #silently ignore missing dependencies
Run Code Online (Sandbox Code Playgroud)

我的问题是-CMP0046的此设置是否会传播到subdir21subdir22

StA*_*nzo 6

否。最好直接从文档中回答这个问题...

策略设置使用堆栈限制范围。进入项目的新子目录(带有add_subdirectory)时,将推送新级别的堆栈,并在退出时弹出该堆栈。因此,在项目的一个目录中设置策略不会影响父目录或兄弟目录,但会影响子目录。

要在不包括sub_directories的情况下对特定级别进行临时更改,可以使用

cmake_policy(PUSH)
cmake_policy(POP)
Run Code Online (Sandbox Code Playgroud)

如果要在subdir21和subdir22中应用该策略,则需要在该目录中显式添加该策略,或考虑将其添加到公共父级。


CAM*_*BAP 5

根据对此答案的评论https://unix.stackexchange.com/a/512695/48776

可以使用全局设置策略 set(CMAKE_POLICY_DEFAULT_CMP0046 OLD)

我尝试了不同的 3.x 版本,它有效。