I'm observing strange CMake behavior. If my project has a header file included to all sources using -include inc.h
by means of ADD_COMPILE_OPTIONS(-include inc.h)
command then changes to the header never detected. Meaning I can change the header, but CMake will never try to recompile the main.cpp
. Am I doing something wrong? Is it a CMake bug? Any workaround?
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.12)
PROJECT(include_test)
SET(CMAKE_CXX_STANDARD 17)
INCLUDE_DIRECTORIES(.)
ADD_COMPILE_OPTIONS(
-include inc.h
)
ADD_EXECUTABLE(include_test main.cpp)
main.cpp
#include <iostream>
int main()
{
foo a;
std::cout << a.bar << std::endl;
return 0;
}
inc.h
struct foo
{
int bar = 1;
double baz = 3;
};
EDIT001:
As @Oliv suggests, when trying to use something like SET_SOURCE_FILES_PROPERTIES(main.cpp PROPERTY OBJECT_DEPENDS inc.h)
Of course it wouldnt work because dependency should be a target and not a file on which the cpp depends so I added following:
ADD_CUSTOM_TARGET(HeaderChanged
DEPENDS
inc.h
COMMENT "Checking if include file has changed")
SET_SOURCE_FILES_PROPERTIES(main.cpp PROPERTY OBJECT_DEPENDS HeaderChanged)
which still results in make[2]: *** No rule to make target 'HeaderChanged', needed by 'CMakeFiles/include_test.dir/main.cpp.o'. Stop.
despite the HeaderChanged
target exists