I've found that when I delete cpp
files from my project using cmake
and ninja
, I can't easily compile it without first completely deleting my build directory and starting from scratch. CMake and/or Ninja apparently squirrels away a number of references to all the cpp
files that it compiles, even deleting the CMake cache before re-running CMake doesn't remove all the references.
Is this a known issue? Is there a solution? I've occasionally just run rm $(grep -R <filename> <builddir>)
, but that's a terrible kludge.
EDIT: It appears I was mistaken, as I have not been able to duplicate this problem. Manually re-running CMake appears to always generate the correct list of .cpp
files, even using GLOB
to generate lists of sources.
Turning my comments into an answer
Collecting your source files with
file(GLOB ...)
Yes, CMake won't know about new or deleted source files when collecting your source files with the
file(GLOB ...)
command. This is a known restriction with CMake. I've changed my CMake project(s) to list all source files individually exactly because of this. Out of convenience I'm still collecting my header files with thefile(GLOB ...)
command.Quoting from CMake's
file()
command documentation:Deleting
CMakeCache.txt
to retrigger ConfigurationJust deleting
CMakeCache.txt
may not be enough to retrigger the CMake configuration. Issue 0014820: warn users about removing only CMakeCache.txt claims you need also to delete allCMakeFiles
directories.From my experience the most reliable way to retrigger the CMake configuration is to touch one of the projects
CMakeLists.txt
files.Note: For
ninja
CMake adds arebuild_cache
target for conveniently running CMake for your project again.Retrigger after Updates from Source Control
Just one thought: if the deletion of source files happens because they were removed from your source control there maybe a workaround that still allows you to use
file(GLOB ...)
on your source files.E.g. if you use GIT you could add the following to your main
CMakeLists.txt
:Disadvantage: It would retrigger configuration which each GIT operation (update, commit, ...).
Some References: