Compile filesize

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 27
Reputation: PC_Nerd is an unknown quantity at this point 
Solved Threads: 0
PC_Nerd PC_Nerd is offline Offline
Light Poster

Compile filesize

 
0
  #1
Dec 30th, 2008
Hi,

I've just been looking at Hello World applications for programmign CGI's - and ive discovered that:

  1. #include <stdio.h>
  2. int main(void) {
  3. printf("Content-Type: text/plain;charset=us-ascii\n\n");
  4. printf("Hello world\n\n");
  5. return 0;
  6. }

Compiles to about 15.4 kb, compared to:
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout<<"Content-type: text/plain"<<endl<<endl;
  6. cout<<"Hello World!"<<endl;
  7.  
  8. return 0;
  9. }

which compiles to about 458kb


Can anyone explain why the two peices of code compiel to such different sizes (Is it the differnet IO library in which case which one is better?)
Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Compile filesize

 
0
  #2
Dec 30th, 2008
Different languages, and different libraries.

> Is it the differnet IO library in which case which one is better?
The C++ one.
There are many more things to worry about than the size of an executable for a small program.

Stuff which would require a lot of code (or more libraries) in C come for "free" in the C++ standard library.

The point is, the executable doesn't grow to 1MB by outputting a 3rd line of text.

As most real-world programs are many 1000's (or millions) of lines of code, the initial hit from the standard library is irrelevant.

Or it could simply be down to debug vs. release, or whether static / dynamic libraries are used, or whether debug information is in the .exe or a separate database.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 27
Reputation: PC_Nerd is an unknown quantity at this point 
Solved Threads: 0
PC_Nerd PC_Nerd is offline Offline
Light Poster

Re: Compile filesize

 
0
  #3
Dec 30th, 2008
Hmmm ok.

both were compiled with the command:
g++ test1.cpp
then they were created into a.exe.

which library is the C++ as opposed to the C?.

Also - is there a specific library list that you would recomend over another set of libraries/files so that filesize is kept to a minimumn ( Im attempting to get as small a filesize as possible...)

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Compile filesize

 
0
  #4
Dec 30th, 2008
std::cout and the streaming to it involve functions from the C++ standard library.

As to libraries to use/avoid to minimise executable size, there's no single correct answer. As Salem said, when you write a substantial program, the size of the executable is more often influenced by the code you write than the libraries you use.

Unless you have severe constraints on available disk space (which is unlikely for people writing a 5 line program) your time would be better spent worrying about getting the program functionality correct than in optimising its performance or disk usage.

Once you've got your program doing what it needs to do, you can turn your attention to optimisation concerns (executable size, runtime speed, etc etc). It is generally more productive to do such optimisations in one hit, after you've got your program working, because the real "hot spots" (the things you need to focus attention on to productively optimise) are not easy to predict without the aid of tools like profilers. One of the most effective and, unfortunately, common ways to become an unproductive programmer is to spend lots of time worrying about optimisation before you need to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 951
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Compile filesize

 
0
  #5
Dec 30th, 2008
If you're desperate for small code, and you're in Windows there's MASM; but that's obviously a very different language from C++.

There's also the -s option and -Os, in GCC.
The manual:
http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/
And a quick primer:
http://tfc.duke.free.fr/coding/gcc-en.html
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,411
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Compile filesize

 
0
  #6
Dec 30th, 2008
>If you're desperate for small code, and you're in Windows there's MASM; but that's obviously a very different language from C++.

And you can use an executable compressor like nPack, see the attachment.

Just another option , but i've used it for compressing some programs/games before, and it works surprisingly well.
Attached Files
File Type: zip EXE Compressor.zip (61.8 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Compile filesize

 
1
  #7
Dec 30th, 2008
Originally Posted by williamhemsworth View Post
And you can use an executable compressor like nPack, see the attachment.
You need to be careful with executable compression. Compressed executables have some characteristics associated with self-modifying code (the decompression stub unpacks code and data into memory). Because of this, they can trigger false positives from anti-virus and anti-malware scanners. Also, to make it worse, a few products out there have actually carried virus or trojan horse payloads.

They can also make some programs run very inefficiently on operating systems that work with virtual memory (eg windows, unix). On those systems, executable compression is not a good idea if multiple instances of an executable are likely to be run concurrently.

If you accept these limitations, one that I've found rather useful is UPX.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC