cmake,在全局变量的概念中丢失(和PARENT_SCOPE或add_subdirectory的替代) [英] cmake, lost in the concept of global variables (and PARENT_SCOPE or add_subdirectory alternatives)

查看:5678
本文介绍了cmake,在全局变量的概念中丢失(和PARENT_SCOPE或add_subdirectory的替代)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cmake项目,其中我有一些模块,我使用Find - *。cmake包括在应用程序中的共享模块。
为了不考虑我添加的每个模块,我已经定义了一个全局的 LIB 变量对链接器:

 #在查找 -  *。cmake中或在模块的CMakeLists.txt中:
set(LIB $ {LIB} ...)

所以在使用某些模块的最终应用程序中,我只能这样做:

  target_link_libraries($ {APP_NAME} $ {LIB})

然后,我想在 / project_path / modules / foo / build 中编译模块,以便如果一个模块真的很大编译它可以编译一次所有使用它的应用程序。我实现这个方法是从Find - *。cmake以这种方式加载CMakeLists.txt的模块:

 #FindFoo.cmake里面,用于加载/project_path/modules/foo/CMakeLists.txt 
#并在/ project_path / modules / foo / build
中编译add_subdirectory($ {CMAKE_CURRENT_LIST_DIR} / .. / modules / $ {PACKAGE_NAME}
$ {CMAKE_CURRENT_LIST_DIR} /../ modules / $ {PACKAGE_NAME} / build

include_directories($ {CMAKE_CURRENT_LIST_DIR} /../ modules / $ { PACKAGE_NAME} / include)

但有时候某些模块需要另外一个模块,因此 add_subdirectory 创建新范围,并且可以正确加载 LIB ,但无法写入(当我使用 set 它是在更深的范围,不改变上限)。对于绕过这个,我必须在 set 中添加 PARENT_SCOPE )..所以我试图添加它在一些模块,我认为可以嵌套并隐藏在一些依赖,但编译所有我突然面临的应用程序:

  CMake Warning(dev)at / path_to_repo / FindFooX.cmake:6(set):
无法设置LIB:当前作用域没有父类。
调用堆栈(最近调用):
CMakeLists.txt:14(find_package)
这个警告是为项目开发者。使用-Wno-dev来禁止它。

恐怕这可能会因应用程式而有所不同

解决方案

CMake中的所有变量都是局部的默认。虽然您可以使用 PARENT_SCOPE 参数将局部变量的范围增加一层,但这对于返回值功能

另一方面,你通常需要一个全局变量的行为:一旦find脚本被任何人调用,你想要的结果是随处可用。特别地,对同一查找脚本的第二次调用应该只重用第一次调用的结果。
在CMake中,这是通过将变量存储到缓存中来实现的。各种 find _ * 调用已自动执行此操作,因此您应该优先使用适当的。对于任何其他自定义变量, 设置 还提供存储到缓存的功能:

 设置(MY_GLOBAL_VARIABLE某些值CACHE STRING 说明)

请注意,局部变量可以隐藏其范围内同名的缓存变量。 / p>

I have a cmake project in which I have some modules and I'm using Find-*.cmake for including the shared modules in the application. For not taking in account every module that I add, I have defined a kind of global LIB variables tor the linker:

# inside a Find-*.cmake or in the CMakeLists.txt of the modules:
set(LIB ${LIB} ...)

so in one of the final applications that uses some modules I can just do:

target_link_libraries(${APP_NAME} ${LIB})

Then, I'd like to have the compiled modules in the /project_path/modules/foo/build so that if a module is really big to compile it can be compiled once for all the application that use it. The way I'm achieving this is to load the CMakeLists.txt of the module from the Find-*.cmake in this way:

# Inside FindFoo.cmake, for loading /project_path/modules/foo/CMakeLists.txt
# and compile it in /project_path/modules/foo/build
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../modules/${PACKAGE_NAME} 
                 ${CMAKE_CURRENT_LIST_DIR}/../modules/${PACKAGE_NAME}/build
)
include_directories(${CMAKE_CURRENT_LIST_DIR}/../modules/${PACKAGE_NAME}/include)

But it happened sometimes that some module require another modules so that the add_subdirectory creates new scopes and can correctly load LIB but cannot write it (when I use set it is in the deeper scope and not changes the upper scope). For bypass this I have to add PARENT_SCOPE in the set).. So I have tried to add it in some module that I think could be nested and hidden in some dependencies but compiling all the application I suddenly faced with:

CMake Warning (dev) at /path_to_repo/cmake/FindFooX.cmake:6 (set):
  Cannot set "LIB": current scope has no parent.
Call Stack (most recent call first):
  CMakeLists.txt:14 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

I am afraid that this can change from app to app in respect to which module I need or in respect of the dependecies tree in the modules itself so I'm looking for a cleaner solution.

解决方案

All variables in CMake are local by default. While you can use the PARENT_SCOPE parameter to increase the scope of a local variable by one layer, that mostly makes sense for return values of functions.

For find scripts on the other hand you usually want the behavior of a global variable: Once the find script is called by anyone, you want the results to be available everywhere. In particular, a second call to the same find script should just reuse the results of the first call. In CMake this is achieved by storing variables to the cache. The various find_* calls already do this automatically, so you should prefer using those where applicable. For any additional custom variables, set offers the capability to store to the cache as well:

set(MY_GLOBAL_VARIABLE "Some value" CACHE STRING "Description")

Note that local variables can hide cached variables of the same name in their scope.

这篇关于cmake,在全局变量的概念中丢失(和PARENT_SCOPE或add_subdirectory的替代)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆