In MS Visual Studio compiler: including standard library header, like
<vector>
or<iostream>
. Does it make a difference in the size of resulting binary if those are included more than once in some.cpp
files compared to including it once in precompiled header (which is eventually getting included in all those and all other.cpp
files in the project)?Same question for non standard library includes. Includes of custom headers of a static library. (If it depends on data size defined in those headers/libraries, then yes, assume there are some definitions.)
What about gcc/Linux environment? Does gcc have precompiled header concept? (If not, consider just some common header that's included in all .cpp files)
1 Answers
Here is the answer for question 1 and 2 combined: (it does not matter if it is custom header or standard header)
If the header does not introduce static global variable, including this header in multiple .cpp
file should not affect the size of resulting binary.
If the header does introduce static global variable, each .cpp
that includes this header with have their own version of static initializer for that variable, adding to the size of resulting binary. One example is cout,cin,clog,cerr
in iostream
.
For this reason, avoid including iostream
in common header/precompiled header (use ostream
instead as it does not introduce static global variable). Include iostream
into .cpp
files that actually need it.
You might also want to try link-time optimization. For MSVC, use /GL /O2
in compile flag and /LTCG
in link flag. For gcc/clang, use -flto
.
(I skipped question 3 since Cody Gray already showed the link in comment)
Reference: https://bugs.chromium.org/p/chromium/issues/detail?id=94794