cmake, lost in the concept of global variables (and PARENT_SCOPE or add_subdirectory alternatives)

前端 未结 2 420
走了就别回头了
走了就别回头了 2020-12-09 05:42

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 t

2条回答
  •  心在旅途
    2020-12-09 06:04

    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.

提交回复
热议问题