0
votes
  1. 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)?

  2. 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.)

  3. 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
No. Precompiled headers is a way of improving compile time.Bo Persson
There should not be a change in the size of the actual executable image. The executable file may differ in size slightly, perhaps, due to slightly different debug data present in the executable file.Sam Varshavchik
The first two questions essentially answer themselves once you know what precompiled headers are. As for the third question: gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.htmlCody Gray

1 Answers

1
votes

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