Mail Archives: djgpp/1997/10/22/14:47:37
student AT nottingham DOT ac DOT uk wrote:
>
> Hi there. Could anyone tell me why, when I compile a 123 byte c++ program
> with the GNU compiler, I get a 200k+ executable?
> This is the test code:
>
> #include <iostream.h>
> #include <conio.h>
It's these two that kill you. A basic "hello, world" program written in
C will compile to about 55k; when you use C++, you add another 50-60k,
and the use of getch() adds another big chunk because the conio
functions come as part of a single library unit.
Basically, in using the full power of a 32-bit protected mode compiler
to print eleven characters to the screen, you are swatting a fly with a
sledgehammer.
There are, however, many ways to reduce the size of DJGPP programs; the
best place to look is in chapter 8.15 of the Frequently Asked Questions
list (v2/faq210b.zip from SimTel or online at
http://www.delorie.com/djgpp/v2faq/). The full explanation is too long
for a simple post, but the basics include:
- Use the '-s' compiler switch to remove the debugging information from
the executable.
- Optimize the code with the '-O' switch and its variants.
- Define empty versions of certain large functions in the startup code
that may not be needed by all programs.
- Use the djp executable packer to runtime compress your programs.
This combination can reduce the final size of a "Hello, World" program
written in C to about 20k. Still, you are wasting a tremendous amount
of effort.
Some additional commentary:
> void main()
This is an illegal construct. main() must always return an integer;
this value is used by the startup code and returned to the operating
system as the program's exit code. If you use the '-Wall' switch in a
C++ program, the compiler will notify you of this.
> {
>
> cout << "Hi there!!\n";
> getch();
Be careful mixing conio functions with stdio functions. This doesn't
bite you here because you printed a newline character, but try omitting
it! As usual, the FAQ has an answer in chapter 9.4.
> // test comment
>
> }
Since main() returns an integer, you need to include a "return 0;"
statement to indicate that your program ran successfully.
> This is the command line to compile it :
>
> gcc -v main.cc -o testcpp.exe -lgpp
Some other good parameters to use, for beginners and advanced users
alike, are '-Wall', to display extra warnings for common programming
mistakes, '-O' to optimize your code (reducing size and increasing
speed), and '-g' to add additional symbol information required if you
attempt to debug your program with a debugger like gdb or RHGDB.
> 200K+ to print a single line and wait for a key seems very odd to me.
> Am I doing some thing wrong, or does gcc add in extra unneccessary
> libraries?
In fact, gcc adds in only what is necessary for your program to run, or
every program would be over 600k long!
hth
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com |
| "Starting flamewars since 1993" | http://www.cs.com/fighteer |
| *** NOTICE *** This .signature is generated randomly. |
| If you don't like it, sue my computer. |
---------------------------------------------------------------------
- Raw text -