file(GLOB ...)
and file(GLOB_RECURSE ...)
only seems to work on the current source directory. Is there any way to glob a different directory?
GLOB will generate a list of all files that match the globbing expressions and store it into the variable. Globbing expressions are similar to regular expressions, but much simpler. If RELATIVE flag is specified for an expression, the results will be returned as a relative path to the given path.
The creators of CMake themselves advise not to use globbing. (We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists. txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.)
A CMAKE file is a project file created by the open-source, cross-platform tool CMake. It contains source script in the CMake language for the entire program. CMake tool uses programming scripts, known as CMakeLists, to generate build files for a specific environment such as makefiles Unix machine.
file(GLOB)
can be a little confusing at first, i had a similar issue a few months ago.
You have to specify your path directly in the <globbing-expressions>
:
file(GLOB <variable>
[LIST_DIRECTORIES true|false] [RELATIVE <path>]
[<globbing-expressions>...])
For example :
file(GLOB my_cpp_list "${CMAKE_CURRENT_SOURCE_DIR}/directory/*.cpp")
foreach(file_path ${my_cpp_list})
message(${file_path})
endforeach()
Will print the path of all .cpp files in ${CMAKE_CURRENT_SOURCE_DIR}/directory
.
I had the same issue where no matter what path I specified, I would get a glob from CMAKE_CURRENT_SOURCE_DIR. The issue was that the path I was providing to GLOB wasn't valid, and it was reverting (gracefully, but annoyingly) to CMAKE_CURRENT_SOURCE_DIR. For example, either of the following will create the symptom you see:
file(GLOB my_cpp_list "${CMAKE_CURRENT_SOURCE_DIR}/directory *.cpp")
I forgot the "/" after "directory". This was why I never got valid results, even with valid directory names.
file(GLOB my_cpp_list "${CMAKE_CURRENT_SOURCE_DIR}/directoryy/*.cpp")
There is no sub-folder named "directoryy"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With