3

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

Community
  • 1
  • 1
user1449029
  • 77
  • 1
  • 2
  • 5
  • 1
    How do you compile your program? – Marc Plano-Lesay Jun 11 '12 at 13:35
  • 4
    Did you build a debug or release version? What compiler are you using? What OS? There are many factors that go into an executable's size. My guess would be you have a debug build. – pstrjds Jun 11 '12 at 13:35
  • Nothing to do with Debug / Release. It's just that `` induces a rather large one-time cost. As the program grows, that cost becomes negligible. – DevSolar Jun 11 '12 at 13:38
  • I'm using Dev-Cpp which is bundled with GCC. I'm running Windows 7 Enterprise. I'm not too sure what you mean by debug build, all I do is click on Compile and Run in the menu. – user1449029 Jun 11 '12 at 13:39
  • 2
    Compiling this using GCC on my GNU/Linux desktop, generates a 7.1kb executable(without any optmization flags). – mfontanini Jun 11 '12 at 13:42
  • Is the anything that I could use in place of in order to reduce the size? – user1449029 Jun 11 '12 at 13:42
  • You can use printf instead of std::cout. And debug in release, with dynamic linking. – Marc Plano-Lesay Jun 11 '12 at 13:43
  • Thanks for the replies. How do I use printf instead of std::cout? What should I replace? Also, how do I use dynamic linking? – user1449029 Jun 11 '12 at 13:48
  • First question: *does it really matter ?* because, believe or not, but the average photo of a modern numeric camera is larger than that. – Matthieu M. Jun 11 '12 at 13:56
  • @user1449029 The information about what compiler/OS you are using should be added to your question. It will help others when trying to answer the question as well as future searchers. – pstrjds Jun 11 '12 at 14:09

5 Answers5

7

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.

flumpb
  • 1,707
  • 3
  • 16
  • 33
4

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.

Walter
  • 44,150
  • 20
  • 113
  • 196
2

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Do not to link you app static. Try to make it dynamic.

rekire
  • 47,260
  • 30
  • 167
  • 264
0

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.

fduff
  • 3,671
  • 2
  • 30
  • 39
  • Hi, how do I switch to release mode? – user1449029 Jun 11 '12 at 13:53
  • if you're using Visual Studio, in your IDE you have default choice to Debug & Release mode, just select the one you want. If you're doing compilation via CLI, -g is for Debug mode, ommitting it is for release. See gcc: http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html – fduff Jun 11 '12 at 14:01