When compiler compiles source (e.g. *.cpp
) file, it creates object file (e.g. *.o
), so that later it will be linked to other .o
and .so
(.lib
files for Windows) files and will constitute the executable file.
Now for analogical situation for not compiling header files each time it creates some .pch files so that it will be linked it by the linker then.
Now if in the scope of a Visula Studio project it is defined a precompiled header, then why Visual Studio complains with an error (e.g. **fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?**
) that the header file is not included into the .cpp file.
By summarizing, here are my questions:
- Why in each .cpp file the precompiled header of the project is necessary?
- How does the requirement of presence of precompiled header in each compilation unit optimizes the compilation process? In other words, what is the benefit of this requirement? (It could be up to the user to decide where to include, where not!)
- If precompiled header is included into a .cpp file, which uses only 2% of what is in .pch file, then the remaining 98% will be added to corresponding .o file to?