I have this C++ file:
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "Hello world!\n";
return 0;
}
After compiling with g++ I get a 913KiB big executable. I was astonished, because I thought g++ would be smart enough to not include any code or data not used by the program from the STL.
Next I used UPX with these settings: upx --overlay=strip. After this the executable size was reduced to 142KiB, a reduction of 85% with no speed penalty (I tested this with more complex, mathematical programs).
According to the upx man page:
An "overlay" means auxillary data atached after the logical end of an executable, and it often contains application specific data (this is a common practice to avoid an extra data file, though it would be better to use resource sections).
I couldn’t find any info that was more specific and was left with the following questions:
– What exactly is this overlay?
– Is it safe to strip?
– If yes, why doesn’t g++ do it, even with -Os?
Contextual information:
- Windows 7 Home Premium SP1 64 bit
- MinGW installed with TDM-GCC
- g++ version 4.5.2
- Compiling with
g++ -Os test.cpp
-Osoptimises the generated code for size, it doesn’t say anything about other non-code segments in the executable file.Did you try the
-slinker option to strip debug symbols, suggested here?