Re: Ridiculous size of executable?

From: tom_usenet (tom_usenet_at_hotmail.com)
Date: 05/12/04


Date: Wed, 12 May 2004 12:21:05 +0100

On Tue, 11 May 2004 18:03:32 -0400, Al puzzuoli <alpuzz@comcast.net>
wrote:

>Hi,
>
>I have been out of the programming arena for 10 years or so, and just
>recently, decided to begin dabbling with C++. As one initial exercise,
>I wrote a very simple program to insure that I understood the syntax of
>a for loop. The code compiles and runs, but I noticed that the size of
>the executable is ridiculous, well over 400 kb.
>I am using the Dev-C++ ide and compiler. I am guessing and hoping that
>there are options for optimization that I just haven't learned yet, or
>are my expectations as to file sizes majorly skewed from my experiences
>back in the days of dos? The code I compiled was as follows:
>
>#include <iostream>
>
>using namespace std;
>int i;
>int main ()
>
>{
>for (i=1; i<=10; i++)
>{
>cout <<i <<endl;
>}
>}

C++ executables consist of a number of things stitched together by the
linker.

1. Object code from your own .cpp files. The size of this can vary
with optimization settings.
2. General C++ runtime support code. This includes routines used by
exception handling, new/delete, RTTI, etc. This is generally a fixed
overhead for a particular compiler, and may be reduced by, e.g.,
disabling exception handling (which is generally unwise).
3. Statically linked libraries, including the C and C++ runtime
libraries. In your case this is the problem - the full C++ runtime
library can be 2 or 3MB, and the part linked into your simple
application up to, say, 1MB.

The problem is that by having #include <iostream> a large amount of
the C++ library is being dragged into your executable, since all of
the locales support, streams, etc., etc. is generally pulled in by the
primitive linker technology being used. Carefully written libraries
can minimize this effect (apparently compiling the above to less than
4K is possible with a carefully put together C++ library of which only
one exists that I know of). There's a paper on this stuff here (read
chapter 3 if you're interested):
http://std.dkuug.dk/jtc1/sc22/wg21/docs/PDTR18015.pdf

The easiest way to reduce exe size is simply to dynamically link with
the C++ library dll. However, this gives a false sense of security,
since the memory requirements of your application are probably now
actually increased by having to load that dll, even though your exe is
much smaller than it was.

Tom

-- 
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


Relevant Pages

  • Re: Different version Access
    ... I will have my first MDB in 2000 and then follow your steps ... Libraries are code-add-ins that provide additional functionality. ... way to break your database. ... If it compiles after the library is unchecked, ...
    (microsoft.public.access.modulesdaovba)
  • g95: undefined symbol
    ... I am building shared libraries to be loaded from within Python. ... All compiles well and runs well with the Intel compiler. ... I can't reasonably keep them merged though, for licensing reasons. ...
    (comp.lang.fortran)
  • Re: Problems compiling with ISE Webpack 8.2.01i
    ... end MAJ3; ... port map,X,A1); ... This code compiles properly and I do see the waveform that I am ... I would like to know more about the libraries and the ...
    (comp.arch.fpga)
  • Re: MEX error message
    ... compiles the stuff with 3.4 which causes some incompatibilities. ... and move the old libraries out of the way, so matlab uses the new, ... Are those runtime libraries just relevant to MEX, ... another solution which comes to my mind: the matlab start script sets up the LD_LIBRARY_PATH. ...
    (comp.soft-sys.matlab)
  • Re: Shared library usage by the process
    ... The usual way for sharing a library involves deferring the linking until ... Some libraries are widely used, like libc and libm, and benefit well from ... to have the same function in each), or between all executables. ...
    (comp.unix.questions)