By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

unwanted functions

P: n/a
If in my project I have a function which is not called,
does linker add this function from object file to final binary file? (not a lib, an executable)

If yes, why? (function has not 'extern')

I ask because if I create classes and inherited classes,
then if only use the polymorphism member functions of inherited class,
in the final binary I will have the mama(?)-class member functions.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bh**********@nic.grnet.gr...
If in my project I have a function which is not called,
does linker add this function from object file to final binary file? (not a lib, an executable)

Strictly speaking linking issues are beyond standard C++, so there is no
"official" answer. Practically speaking most linkers will remove code
that is not referenced. Note that "not being referenced" is different
from "not being called".

For example:
if(some_condition)
{
unused_function();
}

Even if 'some_condition' is always false, but the compiler cannot
determine that at compile time or even if the compiler can detect it at
compile time but doesn't optimize the 'if' clause away,
unused_function() is referenced in the code but won't be called.
Consequently the linker is unable to remove the code of
unused_function(). Because of this you sometimes link much more than you
than you really use.
If yes, why? (function has not 'extern')

I ask because if I create classes and inherited classes,
then if only use the polymorphism member functions of inherited class,
in the final binary I will have the mama(?)-class member functions.


I assume you are talking about base class member functions, though I
like your terminology better :-) Whether the base class member functions
will be removed in the final executable is difficult to tell, it is a
quality of implementation issue. I can imagine that it might be
difficult to remove unused virtual base class functions because of the
vtable. Even if the unused 'mama' functions are not removed it should
not prevent you from using polymorphism, the larger executable size
usually not a problem unless you are targetting an extremely memory
limited platform. Premature optimization is the root of all evil.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Jul 19 '05 #2

P: n/a
<- Chameleon -> wrote:
If in my project I have a function which is not called,
does linker add this function from object file to final binary file?
(not a lib, an executable)
It depends on the linker and the object format.
If yes, why? (function has not 'extern')
It does not really matter if it does or not.
I ask because if I create classes and inherited classes,
then if only use the polymorphism member functions of inherited class,
in the final binary I will have the mama(?)-class member functions.


If the member function is virtual, yes.

Attila aka WW
Jul 19 '05 #3

P: n/a
> > If in my project I have a function which is not called,
does linker add this function from object file to final binary file? (not a lib, an executable)

Strictly speaking linking issues are beyond standard C++, so there is no
"official" answer. Practically speaking most linkers will remove code
that is not referenced. Note that "not being referenced" is different
from "not being called".

For example:
if(some_condition)
{
unused_function();
}

Even if 'some_condition' is always false, but the compiler cannot
determine that at compile time or even if the compiler can detect it at
compile time but doesn't optimize the 'if' clause away,
unused_function() is referenced in the code but won't be called.
Consequently the linker is unable to remove the code of
unused_function(). Because of this you sometimes link much more than you
than you really use.
If yes, why? (function has not 'extern')

I ask because if I create classes and inherited classes,
then if only use the polymorphism member functions of inherited class,
in the final binary I will have the mama(?)-class member functions.


I assume you are talking about base class member functions, though I
like your terminology better :-)


My English is bad, sorry ;-) And I forgot words...
Whether the base class member functions
will be removed in the final executable is difficult to tell, it is a
quality of implementation issue. I can imagine that it might be
difficult to remove unused virtual base class functions because of the
vtable. Even if the unused 'mama' functions are not removed it should
not prevent you from using polymorphism, the larger executable size
usually not a problem unless you are targetting an extremely memory
limited platform. Premature optimization is the root of all evil.


Thanks!
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.