Hello world executable is over 3MB

I am just learning c++ and successfully compiled and ran HelloWorld (I do extensive coding in Matlab, and used to code a bit in C but have basically forgotten C).

I am a bit concerned that the executable is over 3MB in size (the .cpp file is below). Is this reasonable? It seems excessive to my untrained eye.

Some details: I am developing within Code::Blocks using gcc, following along the book 'Beginning programming with C++ for Dummies' by Stephen Davis, and using the software that came with the book's CD.

I found some relevant discussion at these sites:
http://stackoverflow.com/questions/11815005/why-is-a-c-c-hello-world-in-the-kilobytes
http://www.parashift.com/c++-faq/big-exes.html

They implied I might try changing some settings in my compiler. I don't really know how to do such things yet, but I did go into 'Project Build Options' I clicked 'Optimize generated code (for size)'. This didn't change anything.

1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}



Your compiler is statically linking the runtime (that is, all the functions that implement I/O interfaces and the like are being included in the executable). If you Google a bit you can find what flags to pass to link the runtime dynamically. If you do that, you'll notice that your executable is reduced to a much more reasonable size.

But note that if you're going to be sharing your executables with other people, linking statically is more convenient because you don't need to also pass libraries around.
Last edited on
Helios: thanks. I guess I'll just wait until I understand how to control these flags from within the IDE I am using (code::blocks). I'm literally just starting over with HelloWorld. I did use the -Os flag but it didn't do anything.

But perhaps it's best, for now as I mess around with the very basics, not to worry about it.

It sounds like it is supposed to be large like this, and based on the previous links, as I write larger programs the libraries will stay the same size, so it isn't that big a deal.
I did use the -Os flag but it didn't do anything.
It doesn't do anything because that flag is passed to the compiler when it compiles your code. The runtime is already compiled, so there's nothing to optimize.

as I write larger programs the libraries will stay the same size, so it isn't that big a deal.
Exactly.
You could try passing the -s flag to the compiler. It often reduces the size quite a bit but makes debugging impossible more difficult so you probably don't want to have it turned on all the time.
Last edited on
Topic archived. No new replies allowed.