How to set the global variable in a function for cmake?

后端 未结 5 1650
庸人自扰
庸人自扰 2020-12-15 19:14

I\'m writing a CMakeLists.txt to generate files and compile the generated files. I create a function to add some file path strings to a global list variable.

My CMak

相关标签:
5条回答
  • 2020-12-15 19:52

    Another approach is to use global properties. Once you set it:

    set_property(GLOBAL PROPERTY source_list_property "${source_list}")
    

    you can read it from everywhere:

    get_property(source_list GLOBAL PROPERTY source_list_property)
    

    I used in examples above the different names for property (source_list_property) and for variable (source_list). Maybe it is better to use the same name. But point is to use a property as global variables, and not about naming.

    Such global properties aren't in cache.

    0 讨论(0)
  • 2020-12-15 19:53

    PARENT_SCOPE is only for parent, it won't work if you have other non-parent script that want to see it as well.

    You need cache for the true "global-like" variable. In your case, use:

    SET(source_list  "${source_list}" CACHE INTERNAL "source_list")
    
    0 讨论(0)
  • 2020-12-15 20:05

    Another way to address this issue is using macro instead of function - this way all variables inside macro will be evaluated in context of caller.

    0 讨论(0)
  • 2020-12-15 20:12

    Building upon Maxim Suslov's answer, the following code worked for a similar problem I faced:

    set_property(GLOBAL PROPERTY source_list)
    function(add_source)
        get_property(tmp GLOBAL PROPERTY source_list)
        foreach(arg ${ARGV})
            set(tmp "${tmp} ${arg}")
        endforeach()
        set_property(GLOBAL PROPERTY source_list "${tmp}")
    endfunction(add_source)
    
    add_source(a_file)
    add_source(b_file c_file)
    
    get_property(local_prop GLOBAL PROPERTY source_list)
    message("list: ${local_prop}")
    

    Function add_source can be called from inside any sub-directory.

    0 讨论(0)
  • 2020-12-15 20:16

    You need to use set instead of list to affect the variable in the parent scope.

    So replace your list command with:

    set(source_list ${source_list} ${file_path} PARENT_SCOPE)
    
    0 讨论(0)
提交回复
热议问题