Makefile Generator does not correctly record C++ header dependencies.
This issue has been mentioned before, but I am raising again here because I have encountered what is a common situation in C++ projects but where the Makefile generator fails to track dependencies, leading to incorrect rebuilding. The example below leads to correct incremental building with Ninja, but incorrect with the Makefile generator.
Tree structure:
root/
CMakeLists.txt // Adds subdirectories.
exe/
CMakeLists.txt // builds executable.
main.cpp
src/
CMakeLists.txt // builds static library.
my_header.hpp
my_source.cpp
Inside main.cpp
we have:
#include "my_header.hpp"
And this works because in the exe
's CMakeLists.txt file we have:
target_include_directories(
exe
PUBLIC
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/src
)
So a clean build works fine, but the incremental build (under the Makefile generator) fails to rebuild main.cpp
when my_header.hpp
is changed. Indeed, the exe
folder's depend.make
file does not contain a reference to the header file and the CXX.includecache
contains it but with the wrong path.
I have read that the Makefile generator does only approximate preprocessing to deduce header dependencies... but will this ever be fixed? Can we give CMake an option so that the Makefile generator will use the compiler's header dependency emitter? I realize that not all compilers have this ability, but I use only compilers that do have it (such as clang and gcc), and so I think CMake should provide an option for this.
Again, the incremental build works fine under Ninja, so I know it is the fault of the Makefile generator here.
I am using CMake version 3.15.0-rc1.