#### Archived

This topic is now archived and is closed to further replies.

# C/C++ File Size?

## Recommended Posts

BurntToast    122
I''m fairly new to C/C++ programming, and I''ve been mucking around with the basics for a few weeks now, and I''ve noticed something a little curious. If I make a basic C program (just a small main function), and compile it, the executable file comes in at around 6kb. Make a similar program in C++ and compile it, the executable comes out at about 76kb. Why such a huge difference? do C++ programs typically have larger file sizes than comparable C programs, or does it have something to do with the size of the included files (iostream for C++, stdio for C)? Anyone shed any light on this...? just got me wondering... I''m using Bloodshed Dev-C++ as a compiler.

##### Share on other sites
Sneftel    1788
It''s mostly the included files. You''ll find that if you leave out iostream, your file size goes down drastically.

In general, C++ is comparable in size to, or smaller than, C programs with the same design and functionality. But there''s rarely a really significant difference.

##### Share on other sites
Guest Anonymous Poster
Use Alt-P and make sure that the "Generate debug info" checkbox isn''t chosen, it should reduce the size of the exe file.. Sneftel also has a point.. C++ generates huge exe files, especially gcc seems to bloat them even more than VC++

#include <stdio.h>int main(void){   printf("Hello world";   return 0;}

VC - 40960 bytes (release build!! Must have done something wrong??)
GCC - 9685 bytes
LCC - 8774 bytes

#include <iostream.h>int main(){   cout << "Hello world" << endl;   return 0;}

VC++ - 49152 bytes (release build - VC++ 6 sp5 Enterprise)
g++ - 167283 bytes
g++ - 184899 bytes (g++ -g)

Seems to be some difference.. (I''m not an expert on the GCC compiler suite)..

##### Share on other sites
Kylotan    10011
Most of it is iostreams. You may also get a slight overhead from the functions that ensure static objects are properly constructed and destructed, among other things. You might also not be compiling in fully optimised mode, and C++ tends to throw in more debug-type checks than C does, which consumes more space.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]

##### Share on other sites
Null and Void    1088
quote:
Original post by Anonymous Poster
Seems to be some difference.. (I''m not an expert on the GCC compiler suite)..

I got a 5601 byte executable with g++ 2.95.4 (with no optimizations enabled), so you did something horribly wrong.

##### Share on other sites
petewood    819
try it with
  <iosfwd>

can't try it myself. i'm just in a penthouse in bangkok.

of yeah... don't put .h for standard includes

[edited by - petewood on April 28, 2002 11:03:19 PM]

##### Share on other sites
IndirectX    122
By default, VC optimizes release builds for speed. Optimize for size instead, and you can easily cut 1/3 of your build. Also you get large exes because you''re statically linking to CRT libs.

##### Share on other sites
BurntToast    122
Thanks guys - just seemed a little strange and it was bugging me

##### Share on other sites
NuffSaid    122
g++ -s file.cpp

That strips all debugging info from the executable. Reduces the file size to under 10K on my Linux box.

##### Share on other sites
Guest Anonymous Poster
With the -s option the ouput file for that "hello world" example turns out to be 75264 bytes.. I tried reinstalling the compiler and I''ve done exactly like the readme tells me to, but still something must be horrible wrong with it...

g++ main.cpp -> 167283 bytes
g++ -s main.cpp -> 75264 bytes

and that''s the result with gcc-2.95.2

I guess I''ll have to stick with VC++.. Don''t because it''s the best option-but for a dumbass like myself it is the only option.. Muahahaha.. Gonna have to dig even deeper into this subject