0

Let's say we have CMake projet like following:

proj_main
├── proj_sub_0
├── ...
└── proj_sub_n

Each sub-project may have zero or more than one targets. Number of sub-projects may change in situations.

For some reasons, we have to get a list of sub project targets for future use. It's reasonable to setup a list in proj_main and let let each sub-project to append targets into it.

Then here comes the question:

  • Is it to declare an empty list in CMake?
  • How?

UPDATE

As in comments heros mentioned that set(name) could define an empty list, previous questions got resolved.

But, here comes more.

Let's say we defined a list foo and a fucntion func in proj_main/CMakeLists.txt:

...
set(foo_list)
function(func var)
  list(APPEND foo_list var)
endfunction()
...

In proj_sub_i/CMakeLists.txt (where i may be anyone from 0 to n):

...
func(bari)
...

My expectation is that foo_list could be updated by func called in proj_sub_i. But the trueth is foo_list will stays unchanged. And more, even I call foo_list in proj_main, I could not change foo_list at all.

The func calling in proj_sub_i could not effect the foo_list. Why? What's the correct way to update the foo here?

Galaxy
  • 1,129
  • 11
  • 27
  • 3
    Empty variable means empty list: `set(LIST_A)`. – Tsyvarev Nov 15 '19 at 12:17
  • @Tsyvarev Thought the `` is must for `set`. – Galaxy Nov 15 '19 at 12:34
  • How are you using the list? Can you please post the CMake code you are using, at least a minimal example? The syntax suggested by @Tsyvarev is valid CMake language. – Kevin Nov 15 '19 at 12:56
  • 1
    For your updated question see that one: https://stackoverflow.com/questions/22487215/cmake-function-parameter-and-return. It explains how to update outer variable within a function call. Note, that `add_subdirectory` call itself creates a variable's scope. For propagate variable from the subdirectory to the parent one you need again to use `set()` with `PARENT_SCOPE`. – Tsyvarev Nov 15 '19 at 18:04
  • @Tsyvarev Tks, please post asummary as anwser. Notice if you call the function from a sub-directory, you have to call `set(the_list ${the_list} PARENT_SCOPE)` again after the call of the function. If you have more than one level of sub-tree, you have to call `set(the_list ${the_list} PARENT_SCOPE)` in each. – Galaxy Nov 17 '19 at 06:47
  • Despite you concrete questions, your main problem seems to be a creating a **global list**. This is the same problem as asked in [other question](https://stackoverflow.com/questions/35777442/cmake-global-variable-in-macro-scope), so I have marked your one as a duplicate. (Note, that "duplicate" by itself doesn't mean "bad"; another wording for the same problem could be useful for search). – Tsyvarev Nov 17 '19 at 10:00

0 Answers0