Last week I found a simple game on the web called killallcops.exe. I studied the binaries and wondered why the programm is that big. (About 460k)
So I rewrote the game using iostream
#include <iostream>
int main(){
std::cout << "All cops are killed.\nCongratulations";
std::cin.ignore();
return 0;
}
compiled it with g++ and got 460k too.
I really wondered why this thing was so big and rewrote it using stdio
#include <stdio.h>
int main(){
printf("All cops are killed.\nCongratulations");
while(getchar() != '\n'){}
return 0;
}
compiled it with gcc and got 13k.
I know that iostream has much more abilities than stdio (like typesafety) and thus is itself much bigger, but why does this result in bigger binaries and are there solutions to reduce the size when using iostream, for example only using specific parts of it?
edit: as stated below using gcc 3.4.4 under cygwin
<iostream>
has a lot of internal dependencies, even for using a simplecout
call. It brings in a lot of other code/headers it needs to work (which includes template code and such). You can trust the compiler/linker to know what they're doing, but if you want to reduce the binary size, try using options likegcc -s
, which strips symbols, or flags like-Os
which attempts to optimize the binary for size. However, in nontrivial apps the size of bringing in<iostream>
is not very significant. – wkl Dec 24 '11 at 15:31