| Infrequently Asked Questions (IFAQ) |
A: First, remember that the preprocessor (cpp) #includes the iostream.h header file into your source file, before the compiler ever sees the file. iostream.h itself #includes other header files, etc., so several potentially large text files are added to your hello.cpp program before it's ever compiled.
We can use the g++ command to invoke cpp and save cpp's output to a file: g++ -E hello.cpp > pre-hello.cpp
Note the size of the pre-processed file, pre-hello.cpp: 24,894 bytes.
The file has been pre-processed but not yet compiled. Use the "more" command to examine the contents of the file to see what cpp added: more pre-hello
Next, use g++ to compile hello.cpp to the executable, hello: g++ -o hello hello.cpp
Note the size of the executable, hello: 705,576 bytes!
To understand what is going on, note that the compiler allocates space for an executable in three segments: Text, Data, and BSS.
The text segment stores executable statements, the data segment stores initialized variables, and the BSS segment stores variables that don't have a value yet. The size of each of these segments is given by the Unix size command.
Examples:
% size hello 98066 + 15532 + 1516 = 115114To print some headings, use this command:
% echo "text data bss total" ; size a.out text data bss total 98098 + 15532 + 1516 = 115146Still, we're 600,000 bytes short of an answer to Why is a.out so large?
The rest of the answer can be determined by using the Unix strip command, which removes the compiler-generated symbol table and other debugging information from a.out. This information was added to a.out by the compiler, but it is not stored in any of the three segments-- it is extra information used only for debugging tools. Therefore, this command is normally run only on programs that have been debugged and tested.
% strip a.outAfter the call to strip, the size of a.out: 123,360 bytes.
text data bss total 98098 + 15532 + 1516 = 115146Although a.out is still slightly larger than the total of its three segments, the numbers are much closer.