I have a Visual Studio 2017 C++ project which uses precompiled headers (stdafx.h).
My (simplified) file structure is as follows:
header1.hpp
header2.hpp
stdafx.h -> includes header1.hpp
code1.cpp -> includes stdafx.h
code2.cpp -> includes stdafx.h and header2.hpp
To avoid any ambiguity, by saying includes header1.hpp
, I mean there is a line #include <header1.hpp>
in the file.
After I modify
stdafx.h
, all three filesstdafx.h
,code1.cpp
andcode2.cpp
are recompiled (as expected).After I modify
code1.cpp
, onlycode1.cpp
is recompiled (as expected).After I modify
header2.cpp
, all three filesstdafx.h
,code1.cpp
andcode2.cpp
are recompiled (not expected!).
I would have expected item 3 to compile only code2.cpp
, and not everything else.
A possible reason for this behavior would be that the build manager can't inspect and follow #include
directives, so it simply recompiles everything if any header file is modified.
Since my project is quite large, recompiling all the files like code1.cpp
takes up a significant amount of time. I purposely did not include header2.hpp
from stdafx.h
because I know I would modify header2.hpp
often and only code2.cpp
needs to use it. (header2.hpp
is a template library that has functions only code2.cpp
needs.)
Is this behavior expected, and what can I do to get Visual Studio to recognize that the other files need not be recompiled?