[CMake] How to make FILE() append to the list of files?

Eric Noulard eric.noulard at gmail.com
Thu Nov 27 01:58:18 EST 2008

2008/11/27 Robert Dailey <rcdailey at gmail.com>:
>> find . -name "*.c" > source_files.txt
>> edit source_files.txt and put that list of files exiplicitly into a
>> CMakeLists.txt file.
>> file(GLOB is a bad way to get source lists for CMake.   CMake has no way
>> of knowing when new files have been put in the directory.
> But unless I am missing a fundamental feature somewhere, GLOB still seems to
> be the better alternative. While it may not intrinsically know when new
> files have appeared on the filesystem, the programmer can simply re-run the
> CMake command to get an updated project with the newly added source files
> without editing the CMakeLists.txt file directly.

Yes but when he add a source file, he won't necessarily remember he
MUST rerun CMake manually
so its next attempt to "build"  the project will trigger either a
compile error (added header missing)
or link error (added source file not compiled).

Whereas with hard-written sources files in CMakeLists.txt, the user
will get accustomed to
"simple" CMakeList.txt editing thus CMake will relaunch itself
automatically when needed and
in particular when a CMakeLists.txt is changed.

Adding source files with FILE(GLOB may be the way to go for generated
files, but for hand-written
one its a bad habit.

> This is better than the alternative, which is a hard-coded list of project files in the CMake script.

It usually looks like a better alternative (but I may be wrong) when
you are in the process of
converting several project to CMake and you end-up writing a lot of
boring CMakeLists.txt in the startup process :=)

So your solution just works, but it looks like a bad habit.
Now for appending var content you may try to use LIST  CMake command

list(APPEND YourList ${GLOB_RESULT})


More information about the CMake mailing list