Possible Duplicate:
GCC C++ “Hello World” program -> .exe is 500kb big when compiled on Windows. How can I reduce its size?
I've just started reading some C++ online tutorials and the first lesson was the Hello World program. When I compile the program to an executable, the size is over 400kb even though it's just a simple Hello World console program. Should it be this big? If not, why is it happening? Am I doing something wrong?
Here is the source:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
cin.get();
}
Any help would really be appreciated. Thanks
Statically linking the C and/or C++ runtime can greatly increase the size. Also, compiling your program to include debugging information can increase the size.
It probably is with debug information. If you strip that out (by building in release mode in Visual Studio, or using the strip
command in Linux) it will be much smaller.
Do not to link you app static. Try to make it dynamic.
This is almost certainly because you created a static executable, i.e. one which can run standalone and doesn't rely on run-time libraries. See for the documentation of your compiler/linker for how to avoid that.
Edit:
From your code I get 13540 bytes for a dynamically linked executable (gcc 4.3.2 on linux) but 6.7Mb for a statically linked executable.
building in Debug mode will compile and link code that is not optimise in any way (speed/size) and you end up with bloated exec that includes debug version of system libs.
if you switch to release mode, the compiler and linker can be optimised to only use the functions they require in the final exec and also use their release versions.
that way you will end up with 'hopefully' a smaller exec if you've selected optimise for space.