0

I try to configire my CMake project so it can accept dynamic source list. But for some reason I get empty list from file function. So I've tried to execute the same operation in 2 ways:

[1] static list (the unwanted way)

set(VARIANT_A "src/dir1/*.cpp src/dur2/*.cpp src/dir3/*.cpp") 
file(GLOB SRC . ${VARIANT_A})  
messages(${SRC})

here I get the file list I need.

[2] dynamic list (that what I want to do)

set(VARIANT_B "dir1 dir2 dir3")
string(REPLACE " " ";" DirList ${VARIANT_B})
set(Sources "")
foreach(Dir ${DirList})
    set(Sources "src/${Source}/*cpp ${Sources}")
endforeach()
message(${Sources})
file(GLOB SRC . ${Sources})
messages(${SRC})

here I get the empty list for some reason despite the fact that message(${Sources}) prints out the same string as ${VARIANT_A} does.

So what I do wrong?

folibis
  • 12,048
  • 6
  • 54
  • 97
  • Enclosed into **double quotes** a value is **never** a list but just a string (single value). Remove that double quotes. You may examine difference between a list and just a string by `message()` call if you put printed argument into double quotes: `message("{Sources}")`. A list's elements will be printed as separated by semicolon (`;`), otherwise a space will be printed as a separator of words in a string. – Tsyvarev Aug 02 '20 at 16:34

1 Answers1

0

To create a list you do like this

set(VARIANT_A "src/dir1/*.cpp;src/dur2/*.cpp;src/dir3/*.cpp") 
list(LENGTH VARIANT_A iCount)
message( "LENGTH=${iCount}" )

CMake Tutorial

Per Ghosh
  • 449
  • 5
  • 10