| | |
Compile filesize
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 27
Reputation:
Solved Threads: 0
Hi,
I've just been looking at Hello World applications for programmign CGI's - and ive discovered that:
Compiles to about 15.4 kb, compared to:
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
I've just been looking at Hello World applications for programmign CGI's - and ive discovered that:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { printf("Content-Type: text/plain;charset=us-ascii\n\n"); printf("Hello world\n\n"); return 0; }
Compiles to about 15.4 kb, compared to:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { cout<<"Content-type: text/plain"<<endl<<endl; cout<<"Hello World!"<<endl; return 0; }
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
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.
> 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.
•
•
Join Date: Mar 2008
Posts: 27
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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.
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.
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
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Mar 2008
Posts: 1,411
Reputation:
Solved Threads: 114
>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.
And you can use an executable compressor like nPack, see the attachment.
Just another option
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
And you can use an executable compressor like nPack, see the attachment.
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: toolTip1 for textBox
- Next Thread: Joystick
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






