Search code examples
c++g++sizeexecutableobject-files

Difference of Sizes of Various Fields Between Object File and Executable File


I got the following information using the size command in linux, for a sample c++ program.

text    data     bss     dec     hex   filename
1908     304     152    2364     93c   test
 346       4       1     351     15f   test.o

I have read that the output are the sizes; text is the actual code, bss contains the uninitialized global variables, and data as the addition of both initialized and uninitialized segments. My question is why are the sizes differing in the object file and the executable file, specially in text, since source code is common in memory? and if there are no global variables, why is there a size shown for bss?

The sample code file contains are as below:

#include <iostream>

using namespace std;

   int main()
   {
      cout << "Hello World!" << endl ;

      return 0;
   }

Thank You!


Solution

  • The sizes are different because your object file only includes your code. The linked file includes as well compiler C++ framework (i.e. the code needed to call main) and links to various standard libraries (the C and C++ standard libraries at least).