943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 497
  • C++ RSS
Dec 30th, 2008
0

Compile filesize

Expand Post »
Hi,

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

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
PC_Nerd is offline Offline
27 posts
since Mar 2008
Dec 30th, 2008
0

Re: Compile filesize

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 30th, 2008
0

Re: Compile filesize

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
PC_Nerd is offline Offline
27 posts
since Mar 2008
Dec 30th, 2008
0

Re: Compile filesize

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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Dec 30th, 2008
0

Re: Compile filesize

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
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Dec 30th, 2008
0

Re: Compile filesize

>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, 6 views)
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Dec 30th, 2008
1

Re: Compile filesize

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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: toolTip1 for textBox
Next Thread in C++ Forum Timeline: Joystick





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC