c++object-files

On what does the size of an C++ object file depend?


Whenever we compile a c++ file, an obj file is generated. I want to know that on what factors does the size of the obj file depend?

Just to make my question more clear, For example a c++ file contains a class declaration and this class has 1 integer variable as data member and also has some member functions. If i compile this file then some obj file will be created of some X size. Now assume that i add more data members and member functions, then will the size of obj file change?


Solution

  • That depends on a million different factors and is entirely platform and compiler and settings dependent.

    The object file has to contain all the assembly for function bodies for functions with external linkage, as well as all the global variables with external linkage. Anything with internal linkage may or may not warrant a separate entry in the object file, as those may have been optimized out and integrated directly into their call site. This depends heavily on the optimization settings.

    GCC also has an option for "link time optimizations" which essentially adds a copy of the entire source code to the object file and increases its size dramatically.

    Debug symbols also add a lot of extra data.

    For your C++-specific question: A class definition itself isn't really visible in the assembly. Non-inlined member functions are just more functions that have to be compiled, while data members are just treated the same as primitive data members - they'll be on the call stack if you declare instances of that type, but they don't directly impact the assembly code ... unless you're initializing things with constants; constants do go into the code, of course.