Re: A way to decrease executable sizes?
From: Leor Zolman (leor_at_bdsoft.com)
Date: 04/15/04
- Next message: John Carson: "Re: RATIONAL ROBOT TESTER/ CONTRACT/ IMMEDIATE"
- Previous message: Victor Bazarov: "Re: RATIONAL ROBOT TESTER/ CONTRACT/ IMMEDIATE"
- In reply to: Filipe Martins: "A way to decrease executable sizes?"
- Next in thread: Gary Labowitz: "Re: A way to decrease executable sizes?"
- Reply: Gary Labowitz: "Re: A way to decrease executable sizes?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: John Carson: "Re: RATIONAL ROBOT TESTER/ CONTRACT/ IMMEDIATE"
- Previous message: Victor Bazarov: "Re: RATIONAL ROBOT TESTER/ CONTRACT/ IMMEDIATE"
- In reply to: Filipe Martins: "A way to decrease executable sizes?"
- Next in thread: Gary Labowitz: "Re: A way to decrease executable sizes?"
- Reply: Gary Labowitz: "Re: A way to decrease executable sizes?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|