cmake 3.0 , change parent scope variable in side a function
- User
cmake 3.0 , change parent scope variable in side a function
- Assistant
To change a parent scope variable inside a function in CMake 3.0 or higher, you can use the
set()function with thePARENT_SCOPEoption.Here is an example:
function(change_parent_scope_variable) set(PARENT_VARIABLE "new value" PARENT_SCOPE) endfunction() set(PARENT_VARIABLE "old value") message("PARENT_VARIABLE = ${PARENT_VARIABLE}") change_parent_scope_variable() message("PARENT_VARIABLE = ${PARENT_VARIABLE}")In this example, the
change_parent_scope_variablefunction sets thePARENT_VARIABLEto a new value using theset()function withPARENT_SCOPEoption. When the function is called, the parent scopePARENT_VARIABLEis updated with the new value.The output of this example will be:
PARENT_VARIABLE = old value PARENT_VARIABLE = new valueAs you can see, the value of
PARENT_VARIABLEhas been changed inside the function and the change was visible to the parent scope. - User
why i can't change the value inside functin, even when i used PARENT_SCOPE
- Assistant
If you are encountering issues with changing a parent scope variable inside a function even when using the
PARENT_SCOPEoption, it may be due to scoping rules in CMake.When a function is called in CMake, it creates a new scope, and any variables set within that scope are not visible in the parent scope unless the
PARENT_SCOPEoption is used.However, if a variable with the same name already exists in the parent scope, any variable set inside the function will not update the value of the parent scope variable, but will instead create a new variable with the same name only in the local scope of the function.
To update the value of the parent scope variable, you need to explicitly specify the scope of the variable using
${<scope>_VARIABLE}syntax when setting the variable value.Here is an example:
function(change_parent_scope_variable) set(PARENT_VARIABLE "new value" PARENT_SCOPE) endfunction() set(PARENT_VARIABLE "old value") message("PARENT_VARIABLE = ${PARENT_VARIABLE}") change_parent_scope_variable() message("PARENT_VARIABLE = ${PARENT_VARIABLE}")In this example, the
change_parent_scope_variablefunction sets thePARENT_VARIABLEto a new value using theset()function withPARENT_SCOPEoption.However, if you have already set
PARENT_VARIABLEin the parent scope, you can update its value inside the function like this:if(DEFINED PARENT_VARIABLE) set(PARENT_VARIABLE "new value" CACHE INTERNAL "") endif()This will update the value of the parent scope
PARENT_VARIABLEif it already exists.Alternatively, you can use a global variable to store the value, which can be accessed from any scope including functions.
For example:
set(GLOBAL_VARIABLE "old value" CACHE INTERNAL "") function(change_global_variable) set(GLOBAL_VARIABLE "new value" CACHE INTERNAL "") endfunction() message("GLOBAL_VARIABLE = ${GLOBAL_VARIABLE}") change_global_variable() message("GLOBAL_VARIABLE = ${GLOBAL_VARIABLE}")In this example, we set
GLOBAL_VARIABLEas a global variable usingCACHE INTERNAL ""options. This enables us to access the variable from any scope, including functions. When thechange_global_variablefunction is called, it updates the value ofGLOBAL_VARIABLEto "new value" and this change is visible to the rest of the script.