Size of Exe file

D

Dot.Hu

I'm using static libraries and dynamic libraries every day.
Now I want to know when I add one static library into my project, the
libray will be all added into the final exe file, or only the part I
use is added?
Internet linkes or relative reference books are appreciated.
Thanks.
 
M

Maxim Yegorushkin

I'm using static libraries and dynamic libraries every day.
Now I want to know when I add one static library into my project, the
libray will be all added into the final exe file, or only the part I
use is added?

A .lib file is basically an archive of .o/.obj files. The linker links
in only those .obj files from the archive that define the symbols that
are unresolved in the binary being linked. In other words, if you link
against a .lib, but the binary being link does not use any of the
symbols from the .lib, that .lib is not contributing to the size of
the resulting binary.
 
D

Dot.Hu

A .lib file is basically an archive of .o/.obj files. The linker links
in only those .obj files from the archive that define the symbols that
are unresolved in the binary being linked. In other words, if you link
against a .lib, but the binary being link does not use any of the
symbols from the .lib, that .lib is not contributing to the size of
the resulting binary.

Thanks very much, Max.
I got another question.
Once one symbol is used of one .lib, then the whole .lib will be
linked into the exe?

Now I want to create some libraries, where my useful codes will
reside.
But I don't want to increase the size of .exe, because of some unused
symbols.
That's why I post this question.
 
T

Thomas Beckmann

Thanks very much, Max.
I got another question.
Once one symbol is used of one .lib, then the whole .lib will be
linked into the exe?

No, as Max mentionen the .lib is an archive of .obj files. The linker will
select only the subset of object files in the lib that contain symbols
referenced. Thus, unused object files will automatically be thrown away and
do not contribute to the resulting executable.

Thus, in some situation i.e. embedded development it may be worthwhile to
think about the distribution of functions to implementation (.cpp) files.
Now I want to create some libraries, where my useful codes will
reside.
But I don't want to increase the size of .exe, because of some unused
symbols.
That's why I post this question.

If you have a newer compiler chances are there is some function-level
linking option that allows the linker to on a per-function basis rather the
object files. On VC++ link-time code generation will strip the resulting
executable even further.

Regards,
Thomas.
 
D

Dot.Hu

No, as Max mentionen the .lib is an archive of .obj files. The linker will
select only the subset of object files in the lib that contain symbols
referenced. Thus, unused object files will automatically be thrown away and
do not contribute to the resulting executable.

Thus, in some situation i.e. embedded development it may be worthwhile to
think about the distribution of functions to implementation (.cpp) files.


If you have a newer compiler chances are there is some function-level
linking option that allows the linker to on a per-function basis rather the
object files. On VC++ link-time code generation will strip the resulting
executable even further.

Regards,
    Thomas.

Thank you, Thomas.
It's clear now.
One more question, how did you got to know these?
Are there some relative books or websites? So I can learn more rather
than know the basis.
 
T

Thomas Beckmann

Thank you, Thomas.
It's clear now.
One more question, how did you got to know these?
Are there some relative books or websites? So I can learn more rather
than know the basis.

Hard to say where I read these. I recall this was an important topic in the
late 90s, guess the MSDN magazine had an article or two on the topic. Their
website has an archive. Also, http://www.ddj.com is a great source of
information and certainly the Exceptional C++ series of Sutter I can
recommend.
 
D

Dot.Hu

Hard to say where I read these. I recall this was an important topic in the
late 90s, guess the MSDN magazine had an article or two on the topic. Their
website has an archive. Also,http://www.ddj.comis a great source of
information and certainly the Exceptional C++ series of Sutter I can
recommend.

OK, thanks very much.
 
J

Jorgen Grahn

Hard to say where I read these. I recall this was an important topic in the
late 90s, guess the MSDN magazine had an article or two on the topic. Their
website has an archive. Also, http://www.ddj.com is a great source of
information and certainly the Exceptional C++ series of Sutter I can
recommend.

On my system, the linker's documentation (not surprisingly) documents
this.

Just to be clear: it's a feature of the compiler/linker, not the C++
language. I can imagine linkers which just pulls in the needed parts
out of the object files, in turned pulled from the archives. Or
systems without linkers, etc.

/Jorgen
 
J

James Kanze

On my system, the linker's documentation (not surprisingly)
documents this.

Most of the linker documentation I've seen supposes that one
already knows the real basics: what a linker does (in general),
what a library is, etc. If you're really good, you can probably
deduce much of this from the linker documentation, but
typically, I'd hardly call it pedagogical material.
Just to be clear: it's a feature of the compiler/linker, not
the C++ language.  I can imagine linkers which just pulls in
the needed parts out of the object files, in turned pulled
from the archives. Or systems without linkers, etc.

Yes and no. The standard does say at one point (phases of
compilation, phase 9) that "All external object and function
references are resolved. Library components are linked to
satisfy external references to functions and objects not defined
in the current translation. All such translator output is
collected into a program image which contains information needed
for execution in its execution environment." Which sounds
pretty much like a linker to me. And there are generally
established definitions, independently of the implementation (so
under Windows, a DLL is a type of object file, and not a
library, despite its name); the question per se isn't
implementation specific, even if a really detailed answer will
involve some implementation specific details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top