Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

cmake 3.0 , change parent scope variable in side a function

  1. User

    cmake 3.0 , change parent scope variable in side a function

  2. Assistant

    To change a parent scope variable inside a function in CMake 3.0 or higher, you can use the set() function with the PARENT_SCOPE option.

    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_variable function sets the PARENT_VARIABLE to a new value using the set() function with PARENT_SCOPE option. When the function is called, the parent scope PARENT_VARIABLE is updated with the new value.

    The output of this example will be:

    PARENT_VARIABLE = old value
    PARENT_VARIABLE = new value
    

    As you can see, the value of PARENT_VARIABLE has been changed inside the function and the change was visible to the parent scope.

  3. User

    why i can't change the value inside functin, even when i used PARENT_SCOPE

  4. Assistant

    If you are encountering issues with changing a parent scope variable inside a function even when using the PARENT_SCOPE option, 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_SCOPE option 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_variable function sets the PARENT_VARIABLE to a new value using the set() function with PARENT_SCOPE option.

    However, if you have already set PARENT_VARIABLE in 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_VARIABLE if 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_VARIABLE as a global variable using CACHE INTERNAL "" options. This enables us to access the variable from any scope, including functions. When the change_global_variable function is called, it updates the value of GLOBAL_VARIABLE to "new value" and this change is visible to the rest of the script.