Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Linking in C++ (http://www.velocityreviews.com/forums/t747345-linking-in-c.html)

jacob navia 04-25-2011 05:00 PM

Linking in C++
 
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


Victor Bazarov 04-25-2011 05:16 PM

Re: Linking in C++
 
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

Alain Ketterlin 04-25-2011 05:56 PM

Re: Linking in C++
 
jacob navia <jacob@spamsink.net> 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.

jacob navia 04-25-2011 09:20 PM

Re: Linking in C++
 
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.



jacob navia 04-25-2011 09:21 PM

Re: Linking in C++
 
Le 25/04/11 19:56, Alain Ketterlin a écrit :
> jacob navia<jacob@spamsink.net> 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.

James Kanze 04-25-2011 09:53 PM

Re: Linking in C++
 
On Apr 25, 6:16 pm, Victor Bazarov <v.baza...@comcast.invalid> 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

Ebenezer 04-27-2011 06:24 PM

Re: Linking in C++
 
On Apr 25, 4:53*pm, James Kanze <james.ka...@gmail.com> wrote:
> On Apr 25, 6:16 pm, Victor Bazarov <v.baza...@comcast.invalid> 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

jacob navia 04-27-2011 07:56 PM

Re: Linking in C++
 
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 <james.ka...@gmail.com> wrote:
>>> On Apr 25, 6:16 pm, Victor Bazarov <v.baza...@comcast.invalid> 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.

Bo Persson 04-27-2011 08:00 PM

Re: Linking in C++
 
Ebenezer wrote:
> On Apr 25, 4:53 pm, James Kanze <james.ka...@gmail.com> wrote:
>> On Apr 25, 6:16 pm, Victor Bazarov <v.baza...@comcast.invalid>
>> 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



Ebenezer 04-27-2011 11:49 PM

Re: Linking in C++
 
On Apr 27, 3:00*pm, "Bo Persson" <b...@gmb.dk> wrote:
> Ebenezer wrote:
> > On Apr 25, 4:53 pm, James Kanze <james.ka...@gmail.com> wrote:
> >> On Apr 25, 6:16 pm, Victor Bazarov <v.baza...@comcast.invalid>
> >> 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.


All times are GMT. The time now is 04:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.