The CMake documentation explicitly states that file(GLOB ...)
is not
recommended to collect source files for a build, but it doesn't
mention what the recommended method actually is.
Specifying every source file manually sounds a little bit too manually
to me. So, what is the right method to collect source files, if not
file(GLOB ...)
?
I use a conventional CMakeLists.txt and a python script to update it. I run the python script manually after adding files.
Example before:
after:
Manual is indeed the recommended method. By recommending against using GLOB, the documentation is simply warning against a build system that depends on files present. For example, you want to add a test executable, so you create mytest.cpp. Oops. Now your library compilation breaks. The documentation for AUX_SOURCE_DIRECTORY (similar purpose as globbing for for source files) gives the following warning:
If you're certain that you want all the contents of a directory, and don't plan on adding new ones, then by all means use a GLOB.
Also, don't forget listing files manually doesn't have to involve typing all the filenames. You could do, for example,
ls *.cpp >> CMakeLists.txt
, then use your editor to move the list of files to the correct place in the file.I use GLOB for exactly that and every time I add a file I run
The next
make
command will re-scan the directories."There is no way for CMake to generate a build system that knows when a new source file has been added" Really? Okay, so tell it!
It's not 100% automatic but a damn sight easier than adding files manually.
I use cog, a python module. Here is a sample to collect .cpp file:
The CMakeLists.txt:
And then, run:
The CMakeLists.txt file will be updated in place.
For how to install cog and other usage, please read the article from the author.