Variables set with PARENT_SCOPE are empty in the corresponding child scope. Why?

后端 未结 4 2112
轮回少年
轮回少年 2021-02-12 16:17

Consider the following minimal example:

.
├── bar
│   └── CMakeLists.txt
└── CMakeLists.txt

where ./CMakeLists.txt is



        
4条回答
  •  时光说笑
    2021-02-12 17:18

    I do not see anything that is not consistent with the SET command documentation

    If PARENT_SCOPE is present, the variable will be set in the scope above the current scope. Each new directory or function creates a new scope. This command will set the value of a variable into the parent directory or calling function (whichever is applicable to the case at hand).

    ./bar/CMakeLists.txt

    set( BAR "This is bar." PARENT_SCOPE ) #<-- Variable is set only in the PARENT scope
    message( STATUS "Variable BAR in ./bar/ = ${BAR}" ) #<--- Still undefined/empty
    

    You can always do:

    set( BAR "This is bar." ) #<-- set in this scope
    set( BAR ${BAR} PARENT_SCOPE ) #<-- set in the parent scope too
    

    Grep for PARENT_SCOPE in the delivered modules in your installation, for example FindGTK2

    if(GTK2_${_var}_FOUND)
       set(GTK2_LIBRARIES ${GTK2_LIBRARIES} ${GTK2_${_var}_LIBRARY})
       set(GTK2_LIBRARIES ${GTK2_LIBRARIES} PARENT_SCOPE)
    endif()
    

提交回复
热议问题