c++17 "#if __has_include" -- If during most recent build file didn't exist, creating file doesn't trigger re-compilation without other changes
cmake 3.6.2 (-2 Arch) and make 4.2.1 (-1 Arch).
c++17 adds "__has_include" as a preprocessor directive, allowing (among other possibilities) #include'ing a file if it exists, while avoiding an error if it doesn't. This is supported by gcc and clang, and will be supported by Visual Studio.
If the "optional" file doesn't exist at last compilation, and the "optional" file is created, running make doesn't see the file now exists and re-compile. This should be picked up without having to touch the file including the "__has_include", or running make clean.
I think the issue is that CMake isn't adding #include's within a c++17 #if __has_include to a list of files to be monitored, if they don't exist.
CMakeLists.txt
set(CXX_CXX_FLAGS_RELEASE "-std=c++17") add_executable(main main.cpp)
main.cpp
#if __has_include("mightExist.h") #include "mightExist.h" #endif int main() { }
$ mkdir build && cd build && cmake .. && make
(As expected, builds target main.)
$ make
(As expected, shows "Build target main" not re-compiling or re-linking since nothing has changed.)
Now make: ../mightExist.h
this isn't a valid statement so should force a compilation error
$ make
(As NOT expected, shows "Build target main" not re-compiling, improperly thinking nothing has changed.)
If I create an empty mightExist.h, make clean && make, then future changes to only mightExist.h are noticed to trigger re-compilation.
I'll admit I don't understand the internals and interactions of CMake and make. I can tell that make executes CMake, and I'm making the guess that it's CMake that's at least determining which files need to be checked for changes, even if that's done by creating a file make parses.
If I'm wrong and this is a make bug instead, I apologize.