如何检测当前范围在CMake中是否有父级

如何检测当前范围在CMake中是否有父级

本文介绍了如何检测当前范围在CMake中是否有父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检测当前范围是否有父级?

Is there any way to detect if the current scope has a parent?

我有一个项目可以是独立项目,也可以是另一个项目的子项目。 。为了允许子项目使用,我使用PARENT_SCOPE标志将set()推到父级。但是,当构建为独立项目时,会收到当前作用域没有父对象警告。我想通过检测是否存在父项并将set()调用包含在if语句中来避免该错误。还是只有在有父级的情况下,才可以在父级范围内设置变量?

I have a project that can either be a standalone project or a sub-project of another. To allow the sub project case, I use the PARENT_SCOPE flag to set() to push things up to the parent. However, when build as a standalone project I get a "current scope has no parent" warning. I would like to avoid that error by detecting if there is a parent and enclosing the set() calls in an if statement. Or is there another way to set a variable at parent scope only if there is a parent?

推荐答案

我认为最可靠的方法是使用目录属性。

I think the most robust approach is to use the PARENT_DIRECTORY directory property.

无论在 project 命令之前还是之后调用,这都会产生正确的答案。 ,而不管父母和孩子的名字都相同。

This will yield the correct answer regardless of whether it's called before or after the project command, and regardless of whether the parent and child both have the same project name.

get_directory_property(hasParent PARENT_DIRECTORY)
if(hasParent)
  message(STATUS "Has a parent scope.")
else()
  message(STATUS "Doesn't have a parent scope.")
endif()

这篇关于如何检测当前范围在CMake中是否有父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:56