Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Linking in C++

Reply
Thread Tools

Linking in C++

 
 
jacob navia
Guest
Posts: n/a
 
      04-25-2011
Suppose I have some class "derived" that inherits from "mainClass".

If I call just one method from "derived", do my executable include:

(1) Only that method from derived since I do not instantiate any
mainClass objects

(2) All methods of the derived class + constructor/destructor of mainClass

(3) All methods of both classes and all methods of (possible) superclasses.

Thanks

jacob

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-25-2011
On 4/25/2011 1:00 PM, jacob navia wrote:
> Suppose I have some class "derived" that inherits from "mainClass".
>
> If I call just one method from "derived", do my executable include:
>
> (1) Only that method from derived since I do not instantiate any
> mainClass objects
>
> (2) All methods of the derived class + constructor/destructor of mainClass
>
> (3) All methods of both classes and all methods of (possible) superclasses.


This is not governed by the language standard. There are linkers that
will remove unused functions from the object modules (so you're going to
end up with "1"). Other linkers will leave in the entire object module
(so you're getting "2" or "3" depending on how your code is organised).

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      04-25-2011
jacob navia <(E-Mail Removed)> writes:

> Suppose I have some class "derived" that inherits from "mainClass".
>
> If I call just one method from "derived", do my executable include:
>
> (1) Only that method from derived since I do not instantiate any
> mainClass objects


The called method will be included (unless it has been inlined by the
compiler).

> (2) All methods of the derived class + constructor/destructor of mainClass


Other methods are not needed, but ctor and dtor of both "derived" and
"mainClass" are actually used (instantiating "derived" indirectly
instantiates "mainClass").

> (3) All methods of both classes and all methods of (possible) superclasses.


This may happen as well...

Actually your problem has nothing to do with c++, but everything to do
with both the compiler and the linker. What will end up in the
executable depends on the amount of inlining performed by the compiler
and the smartness of the linker (i.e., its ability to notice that some
routines are unused).

-- Alain.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      04-25-2011
Le 25/04/11 19:16, Victor Bazarov a écrit :
>
> This is not governed by the language standard. There are linkers that
> will remove unused functions from the object modules (so you're going to
> end up with "1"). Other linkers will leave in the entire object module
> (so you're getting "2" or "3" depending on how your code is organised).
>


Thanks for your answer Victor.


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      04-25-2011
Le 25/04/11 19:56, Alain Ketterlin a écrit :
> jacob navia<(E-Mail Removed)> writes:
>
>> Suppose I have some class "derived" that inherits from "mainClass".
>>
>> If I call just one method from "derived", do my executable include:
>>
>> (1) Only that method from derived since I do not instantiate any
>> mainClass objects

>
> The called method will be included (unless it has been inlined by the
> compiler).
>
>> (2) All methods of the derived class + constructor/destructor of mainClass

>
> Other methods are not needed, but ctor and dtor of both "derived" and
> "mainClass" are actually used (instantiating "derived" indirectly
> instantiates "mainClass").
>
>> (3) All methods of both classes and all methods of (possible) superclasses.

>
> This may happen as well...
>
> Actually your problem has nothing to do with c++, but everything to do
> with both the compiler and the linker. What will end up in the
> executable depends on the amount of inlining performed by the compiler
> and the smartness of the linker (i.e., its ability to notice that some
> routines are unused).
>



Thanks for your answer Alain.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-25-2011
On Apr 25, 6:16 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 4/25/2011 1:00 PM, jacob navia wrote:


> > Suppose I have some class "derived" that inherits from
> > "mainClass".


> > If I call just one method from "derived", do my executable
> > include:


> > (1) Only that method from derived since I do not instantiate
> > any mainClass objects


> > (2) All methods of the derived class +
> > constructor/destructor of mainClass


> > (3) All methods of both classes and all methods of
> > (possible) superclasses.


> This is not governed by the language standard.


It is at least partially, since you don't have to provide
implementations for functions that are not "used". (The
original question didn't provide enough information for us to
state whether some other functions were "used" or not. In
general, a virtual function is "used" as soon as there is an
instance of the class, regardless of whether the function is
ever actually called.)

> There are linkers that will remove unused functions from the
> object modules (so you're going to end up with "1"). Other
> linkers will leave in the entire object module (so you're
> getting "2" or "3" depending on how your code is organised).


The granularity of most linkers is the object file. In general,
if you are writing a library, you put each non-virtual function
in a separate object file, and only those which are actually
called are included. (But this is, of course, a quality of
implementation issue for the library provider.)

--
James Kanze
 
Reply With Quote
 
Ebenezer
Guest
Posts: n/a
 
      04-27-2011
On Apr 25, 4:53*pm, James Kanze <(E-Mail Removed)> wrote:
> On Apr 25, 6:16 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>
> > There are linkers that will remove unused functions from the
> > object modules (so you're going to end up with "1"). *Other
> > linkers will leave in the entire object module (so you're
> > getting "2" or "3" depending on how your code is organised).

>
> The granularity of most linkers is the object file. *In general,
> if you are writing a library, you put each non-virtual function
> in a separate object file, and only those which are actually
> called are included. *(But this is, of course, a quality of
> implementation issue for the library provider.)
>


This seems like a "premature optimization." I haven't come
across this much, but recall your mentioning it previously.


Brian Wood
Ebenezer Enterprises
http://webEbenezer.net
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      04-27-2011
Le 27/04/11 21:01, Pete Becker a écrit :
> On 2011-04-27 14:24:21 -0400, Ebenezer said:
>
>> On Apr 25, 4:53 pm, James Kanze <(E-Mail Removed)> wrote:
>>> On Apr 25, 6:16 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>>>
>>>> There are linkers that will remove unused functions from the
>>>> object modules (so you're going to end up with "1"). Other
>>>> linkers will leave in the entire object module (so you're
>>>> getting "2" or "3" depending on how your code is organised).
>>>
>>> The granularity of most linkers is the object file. In general,
>>> if you are writing a library, you put each non-virtual function
>>> in a separate object file, and only those which are actually
>>> called are included. (But this is, of course, a quality of
>>> implementation issue for the library provider.)
>>>

>>
>> This seems like a "premature optimization." I haven't come
>> across this much, but recall your mentioning it previously.
>>

>
> Not in a general-purpose library. If you get it wrong your customers
> complain about code bloat. As an extreme example, linking the entire C
> standard library into every application would not be a good thing.
>


For some people all optimizations are "premature" and they deliver
bloated, unoptimized programs. Code bloat slows down programs because
it forces more main memory reads, and makes code cacheing less
efficient. It is a very important optimization.
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      04-27-2011
Ebenezer wrote:
> On Apr 25, 4:53 pm, James Kanze <(E-Mail Removed)> wrote:
>> On Apr 25, 6:16 pm, Victor Bazarov <(E-Mail Removed)>
>> wrote:
>>
>>> There are linkers that will remove unused functions from the
>>> object modules (so you're going to end up with "1"). Other
>>> linkers will leave in the entire object module (so you're
>>> getting "2" or "3" depending on how your code is organised).

>>
>> The granularity of most linkers is the object file. In general,
>> if you are writing a library, you put each non-virtual function
>> in a separate object file, and only those which are actually
>> called are included. (But this is, of course, a quality of
>> implementation issue for the library provider.)
>>

>
> This seems like a "premature optimization." I haven't come
> across this much, but recall your mentioning it previously.
>


Some compilers will do it for you, and put each function in its own
section. Perhaps that's why not everyone have noticed any "bloat".


Bo Persson


 
Reply With Quote
 
Ebenezer
Guest
Posts: n/a
 
      04-27-2011
On Apr 27, 3:00*pm, "Bo Persson" <(E-Mail Removed)> wrote:
> Ebenezer wrote:
> > On Apr 25, 4:53 pm, James Kanze <(E-Mail Removed)> wrote:
> >> On Apr 25, 6:16 pm, Victor Bazarov <(E-Mail Removed)>
> >> wrote:

>
> >>> There are linkers that will remove unused functions from the
> >>> object modules (so you're going to end up with "1"). Other
> >>> linkers will leave in the entire object module (so you're
> >>> getting "2" or "3" depending on how your code is organised).

>
> >> The granularity of most linkers is the object file. In general,
> >> if you are writing a library, you put each non-virtual function
> >> in a separate object file, and only those which are actually
> >> called are included. (But this is, of course, a quality of
> >> implementation issue for the library provider.)

>
> > This seems like a "premature optimization." *I haven't come
> > across this much, but recall your mentioning it previously.

>
> Some compilers will do it for you, and put each function in its own
> section. Perhaps that's why not everyone have noticed any "bloat".
>



What compilers do this?
What I take away is to put a limit on the size of files.
I think this is a reminder of the benefits of moderation.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox and OE linking opening me Firefox 21 01-12-2005 08:31 PM
peer to peer linking and sharing =?Utf-8?B?QmlsbEM=?= Wireless Networking 2 08-23-2004 08:23 PM
not linking msvcr70.dll ASP .Net 0 08-11-2004 01:07 AM
Directly linking to files such as MSWord willy_gates Firefox 0 05-14-2004 11:12 AM
Linking Firebird / Thunderbird jimm67(nospam) Firefox 0 07-06-2003 01:46 PM



Advertisments