Re: A way to decrease executable sizes?

From: Leor Zolman (leor_at_bdsoft.com)
Date: 04/15/04


Date: Thu, 15 Apr 2004 02:15:33 GMT

On 14 Apr 2004 17:25:44 -0700, filipe.martins@free-spy.net (Filipe Martins)
wrote:

>Hello.
>
>I've read somewhere that the executable is smaller if we use a source file
>for each function!
>So, I tested this with gcc and it seams to confirm! What seams to happen is
>that if we call a function from a source-files that defines 3 others, the
>linkers includes the code of all the 4 functions, even if the on we call
>doesn't rely on the others!
>
>What do you people think about this?
>Is there any way to make the linker reject all the code that isn't needed?
>
>If there isn't any other answer to this, I may in the future create a small
>app that could be used in release mode to separate all the functions in
>their own files and compile all of it.
>
>
>Please state your opinions and theorys about this.
>
>PS: If someone wants it I can make the test project available.

First of all, this is borderline off-topic because it really isn't a
language issue. But it is something that comes up and folks who create
projects should be aware of the general approach that the tools take.

My experience is not incredibly up-to-date with respect to the latest
tools and/or project configurations being used, but here's how I see it:

When you create a project that is composed of several primary source files,
the usual scenario is that all the functions in all the source files are
important parts of your program. If you use command line tools and give a
command such as this:

cl app.cpp more.cpp more2.cpp more3.cpp

then the compiler/linker driver (in this case it would happen to be MSVC's)
compiles all the cpp files into obj files, and then links them all into a
single executable. IOW, it doesn't bother checking for dependencies;
evidently, this is the same as the behavior you were seeing with gcc.

To create object files composed of many general-purpose functions, you'd
typically use some sort of library manager utility to create a special kind
of object file: a "library" file. On Win32/etc., these would have the
".LIB" extension, on Unix they'd be .a files, etc. When these library files
are provided on the command line:

cl app.obj more.obj lib1.lib lbi2.lib

then the extension clues in the linker that each and every function within
the libraries is /not/ necessarily one we want, and it only selects those
functions that are actually needed. IOW, functions are loaded based on
dependencies. That's why only the functions you actually /use/ out of the
Standard Library get loaded: they come from library files, not plain old
object files.

So to make a long story stay long, if you want the linker to pick and
choose from an object file, make it a library rather than a plain old
object file.

Disclaimer: I'm sure there are all sorts of special cases, different
extensions, etc., that would make some or most of what I've just said wrong
in some context, but I hope the basic idea is apropos.
        -leor

-- 
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix  
C++ users: download BD Software's free STL Error Message Decryptor at:
   www.bdsoft.com/tools/stlfilt.html


Relevant Pages

  • Re: Cannot activate incremental link - please help
    ... libraries to link against list. ... I discovered that this caused the linker to think that project settings ... > lot of source files that are compiled and linked using command-line ... >>Incremental linking doesn't work when you change a library in your ...
    (microsoft.public.dotnet.languages.vc)
  • Re: skip compilation of unused functions
    ... granularity of the units that the linker can extract from a library is ... section for every function in an object file. ... and the huge number of source files turned it into an ... provide a list of unused functions and variables. ...
    (comp.lang.c)
  • Re: data transfer
    ... Files that end with '.src' or '.c' are source files, raw material in source form for the compiler to turn into relocatable code that the linker can use. ... Files that end in 'obj' and perhaps other extensions depending on your system are object code, assembled from pieces -- your own code and code from libraries -- by the linker. ...
    (comp.dsp)
  • Re: How can I use muti- projects in one program?
    ... create a library from the resulting object file. ... libraries. ... Refer to that directory when compiling (so that the compiler ... Set the compiler and linker options such that the compiler ...
    (comp.lang.fortran)
  • Re: Questions about linking.
    ... My other question is about static linking. ... In other words, does the linker select the necessary stuff, not the ... During the linking phase the linker searches the libraries for unresolved symbols and when it finds them extracts a copy of the object file which is then linked as any object file in input to the linker. ...
    (comp.os.minix)