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

3

9

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?

Southing answered 6/9, 2011 at 11:56 Comment(2)
I would not attempt to draw any conclusions between the "obj" file size and the size of the object (memory) size. What is it that you're trying to achieve?Center
Here i am interested with obj file size as i know that object size will depend on the data members which it has. But can you tell me that on what factors does obj file size depend? Actually i was asked this in an interview so wanted to know the answer.Southing
A
10

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.

Anticipation answered 6/9, 2011 at 12:2 Comment(0)
H
3

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.

Hosea answered 6/9, 2011 at 12:4 Comment(0)
Y
2

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.

Ypsilanti answered 6/9, 2011 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.