0

My source project directory is something like this,

|── CMakeLists.txt
|
├── src
|   |
|   |
|   |
|   └── a
|   |   |
|   |   |__ a.cpp
|   |
|   |
|   |
|   └── b
|       |
|       |__ b.cpp
|
|___test
      |
      |__test.cpp

I need to make a executable out of source a.cpp and b.cpp,

file(GLOB_RECURSE SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*/ *.cpp)

I expect this to take a.cpp and b.cpp as the only source files, but when I print

message("All source file - ${SRC_FILES}")

It picks up test.cpp as well, which I don't know why it's happening. My understanding here is ${CMAKE_CURRENT_SOURCE_DIR}/src/*/ *.cpp, in this statement due to wildcard * it would go through each subdirectory of src folder, and then with *.cpp it would append all the cpp files inside each of these subdirectory.

  • Some studying on what wildcards do would probably be beneficial. If it's in the src directory at all and ends with cpp, it gets picked up. This is not weird behavior in the slightest. – sweenish Nov 17 '20 at 21:16
  • Isn't my wildcard right ? I expect it to search in all the folder inside the src, I am not even givng the path of the test folder. – Punit Salian Nov 17 '20 at 21:18
  • Why don't you explain what you think the wildcards are doing. It will at least highlight where your confusion lies. – sweenish Nov 17 '20 at 21:20
  • Sure let me edit the question – Punit Salian Nov 17 '20 at 21:25
  • @sweenish I have edited the question to tell my understanding – Punit Salian Nov 17 '20 at 21:33
  • 1
    A **space** between `${CMAKE_CURRENT_SOURCE_DIR}/src/*/` and `*.cpp` means that you have **two globbing expressions**. And file `test/test.cpp` matches the last one... The correct globbing expression, according to the [documentation](https://cmake.org/cmake/help/latest/command/file.html#glob-recurse) and its example, is `${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp`. – Tsyvarev Nov 17 '20 at 21:41
  • @Tsyvarev Thanks a lot for pointing that :) – Punit Salian Nov 17 '20 at 21:54
  • 1
    MIght as well bring up this from the documentation as well: We do not recommend using GLOB to collect a list of source files from your source tree. – sweenish Nov 17 '20 at 22:02

0 Answers0