unwanted functions

G

Guest

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.
 
A

Attila Feher

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
 
P

Peter van Merkerk

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.
 
G

Guest

If in my project I have a function which is not called,
(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.


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!
 

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,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top