Does C ++ code split into multiple translation units the overhead of the executable size?

I have code shared by several projects in a static library. Even with a binding to the functional level, I get more object code than I would like in the output - see Another question about this .

Of course, the easiest solution to reduce the amount of object code associated with the final executable would be to split the translation units so that I get more .obj files with less object code. I can even go to extremes - put each function in a separate translation unit.

Suppose I don't care about the mess caused by having ten times as many .cpp files, and I don't care about a possible increase in link time.

Will such a partition into many object files impose an overhead on the executable size? Will the executable file become larger simply because it has ten times as many .obj files (but in general they have exactly the same functions and variables)?

+4
source share
3 answers

Things that are more likely to affect your final EXE size (not a complete list):

  • If you are linking to libraries statically or dynamically (the dynamic link is smaller because the library code is not inside your EXE)
  • You can use many classes of templates, for example. vector <A>, vector <B>, vector <C> will cause bloat code, since each instance of a vector with different types is compiled separately
  • Compiler settings, for example. optimization, size and speed, inlining (a lot of embedding can make the code bigger), optimization of the whole program, if supported
  • Linker settings, for example. if supported, delete redundant or identical data. May help reduce size.

In a code with a short split to more translation units, there will probably be no effect - the same code, just reorganized. This can even make matters worse if your compiler does not take into account the optimization of the entire program, since each translation unit has less information about your program.

+2
source

I think that with modern compilers the gain in size will be too thin. As far as I know, compilers use only those functions that are specified in your code. Other functions and characters are skipped from the output file.

+2
source

Not. I think you need to recompile your static library as a shared library or use something like: strip -g -s -R.comment to remove code that is not in use.

+1
source

Source: https://habr.com/ru/post/1302976/


All Articles