Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 299
V K Avatar asked Sep 06 '11 11:09

V K


3 Answers

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.

like image 141
Kerrek SB Avatar answered Nov 03 '22 01:11

Kerrek SB


The object file contains (among others) the machine code after your source has been compiled, but before it is linked. The size of the object file therefore basically depends on the complexity of the code.

Most compilers gives you the option of adding debugging symbols during compilation to make debugging easier, but will increase the size of your object file. You can add the debug symbols with the -g option and strip them with the -s option in GCC. Visual Studio has something similar.

like image 25
Wernsey Avatar answered Nov 03 '22 00:11

Wernsey


It is heavily compiler dependent, but.

The object file will contain executable code, linkage stubs and metadata. Metadata can be whatever compiler dependent stuff, so it's out of the question - anything can happen to it depending on what the compiler thinks. Linkage stubs are created for all entities with external linkage and and for all outgoing references to entities in other object files - that isn't directly affected by adding members into the class in the same object file.

Now to code. When you add a member variable it has to be constructed and destroyed - that will require a constructor and a destructor both of which might be trivial or not. If they are not trivial this will lead to slight growth in code size since now something extra must be done to deal with those member variables and that requires extra code.

like image 44
sharptooth Avatar answered Nov 03 '22 00:11

sharptooth