2

In our C++ project, we have several CMakeLists.txt files (on different directories) listing every single cpp file desired with target_sources().

For example:

target_sources(<Project> PUBLIC
    foo_1.cpp
    foo_2.cpp
    foo_3.cpp
)

This is fine in case we have few source files, but it's becoming harder for those directories where we have multiple cpp files to be added.

Is there a simple way to tell target_resources() to add all files on directory and subdirectories where CMakeList.txt file is?

For example, placing a CMakeLists.txt on a directory with multiple files and sub-directories (with more files) that just adds everything contained there.

Kevin
  • 16,549
  • 8
  • 60
  • 74
Adso4
  • 111
  • 3
  • 10
  • 1
    You can but it's not recommended: https://stackoverflow.com/questions/1027247/is-it-better-to-specify-source-files-with-glob-or-each-file-individually-in-cmak – Alan Birtles Mar 05 '20 at 08:27

1 Answers1

1

This is solving my problem:

file(GLOB SRC_FILES    
    "*.cpp"
)

target_sources(<project> PUBLIC
    ${SRC_FILES}
)
´´´
Adso4
  • 111
  • 3
  • 10
  • 2
    If you use this [`GLOB`](https://cmake.org/cmake/help/latest/command/file.html#filesystem) command, you should at least add `CONFIGURE_DEPENDS`. It can help avoid some of the unintended or non-obvious consequences of using this command. – Kevin Mar 05 '20 at 12:43