Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add all files under a folder to a CMake glob?

I've just read this:

CMake - Automatically add all files in a folder to a target?

With the answer suggesting a file glob, e.g.:

file(GLOB "*.h" "*.cpp")

now, what if I want my target to depend on all files of a certain type under a certain folder - which might be within multiple subfolders? I tried using

execute_process(COMMAND find src/baz/ -name "*.cpp" OUTPUT_VARIABLE BAR)

and then

add_executable(foo ${BAR}

but this gives me the error:

Cannot find source file:

  src/baz/some/file/here

src/baz/some/other_file/here

src/baz/some/other_file/here2

(yes, with that spacing.)

What am I doing wrong here?

like image 987
einpoklum Avatar asked Feb 15 '16 14:02

einpoklum


People also ask

How do I include a folder in Cmakelist?

Add include directories to the build. Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory. The include directories are added to the INCLUDE_DIRECTORIES directory property for the current CMakeLists file.

What does glob do in CMake?

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.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.


1 Answers

Turning my comment into an answer

If you want to add recursive searching for files use file(GLOB_RECURSE ...)

file(GLOB_RECURSE source_list "*.cpp" "*.hpp")

Your second example would translate into

file(GLOB_RECURSE BAR "src/baz/*.cpp")

References

  • file(...)
  • Specify source files globally with GLOB?
like image 88
Florian Avatar answered Oct 20 '22 05:10

Florian