Are unused class methods removed from exe?

T

ThazKool

I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ


You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.
 
R

red floyd

ThazKool said:
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ


You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.

May I suggest that you ask in a forum dedicated to your compiler?
That's strictly an implementation issue, beyond the scope of the language.
 
T

ThazKool

I said: If it is only specific compilers could you name which ones.
Thanks
again.

This comment was directed to all C++ users. If you are looking to
flame someone, then take a look inside yourself and find out where that
inner child was wounded. Then nurture that inner child and tell him
everything is going to be okay and that he does not have to lash out at
people.

Ideally this should be cross-compiler and cross platform, but I am not
sure and I need the comments of all c++ users. Hence comp.lang.c++.
 
R

rolkA

Hi,
This is the job of the linker.
If a function is not called, its code is not included in the final exe.

You can experiment this :
define a method, but don't implement it and never call it
link --> no error
now, call it somewhere, and link --> unresolved external
So, you see that the linker ignores functions that are never called.

For the "specific compiler" part, i dont really know, but IMHO, no
linker is so stupid to include code parts there are never called.

I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ


You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.


-- -----------------------------
Samuel Lacroix
 
I

Ian Collins

ThazKool wrote:

Please don't top post, it's considered rude in these parts.
I said: If it is only specific compilers could you name which ones.
Thanks
again.

This comment was directed to all C++ users. If you are looking to
flame someone, then take a look inside yourself and find out where that
inner child was wounded. Then nurture that inner child and tell him
everything is going to be okay and that he does not have to lash out at
people.

Ideally this should be cross-compiler and cross platform, but I am not
sure and I need the comments of all c++ users. Hence comp.lang.c++.
I don't think any compiler would do this, it isn't the compiler's job.

That's why the question is OT here, you should ask on a platform
specific group, the platform's tools will look after linking.
 
J

Jerry Coffin

@i40g2000cwc.googlegroups.com>, (e-mail address removed)
says...
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

[ ... ]
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

Most linkers are smart enough to ignore non-virtual
functions that aren't called. It's more difficult to
figure out whether a virtual function is called, so
they're much more likely to be included even if they're
not called.

Except strictly from a viewpoint of the size of
executable, virtual memory makes this less important than
it once was. With demand-paged virtual memory, the code
for the function won't normally be loaded into memory
until or unless it's called (unless it lives in the same
page with something else that's called, in which case it
doesn't usually make a lot of difference).

For anything more specific, you'll probably need to ask
in a newsgroup specific to the platform you're targeting.
 
K

Kaz Kylheku

ThazKool said:
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ


You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

What you can typically expect from the crufty compiler and linker
technology is roughly this:

1. Individual object files that are not referenced are removed during
linking.
2. If an object file contains any external symbol that is referenced
somewhere, the
entire object file is linked to the target, bringing in all the
unused parts with it.
3. The compiler itself may do a good job removing unused functions and
objects
which are private to a compilation unit (declared static).
4. Unused inline functions are usually treated well.

The reason for 2 is that many object file formats simply cannot be
edited to delete a function. They may contain internal references among
the functions which are already resolved: for instance,
program-counter-relative branches from one function to another. The
compiler emits the translation unit image all at once, and knows the
displacement between all the instruction. Calling a function can then
be done using a relocatable relative branch: ``call the function that
is 1549 bytes up from this point''. To delete some code in between
would require all those references which jump across that code to be
decoded and patched, which would require the linker to understand that
machine language.

The last time I looked at the sources for the GNU C compiler, the build
system used a gross hack in order to build the run-time support library
libgcc.a in order to make the library as small as possible. #ifdef
statements were used to divide up the library source file into sections
containing a single function, and it was passed through the compiler
many times, with different preprocessor symbols defined to select
individual functions for compilation, resulting in a separate .o file
for each function. Thus if that run-time function is not used, it
doesn't end up linked to the executable.

If your compiler doesn't support function-level linking, and you
really, really want to remove unused functions, what you can do is put
them into separate source files. Have the class declaration in a single
..h of course, but the implementations of the member functions can be
split into multiple files.

Watch out for virtuals. If a function is virtual, then typically, it's
as good as called. Even if you don't use that function, most compilers
generate object code that requires the functions to be there (e.g. a
virtual containing a symbolic reference to the function which must
resolve).
 
T

ThazKool

Excellent post. This was the insight that I was in search of. I
understand that there are many ways in which compilers implement these
features, but you gave me a great understanding of the way object code
can bloat. I guess some linkers may try to scan for dependencies and
remove unused code, but this can be tricky if the code contains
function pointers which would take us back to the point that object
code can bloat.

Thank you very much. I will still have to experiment some.

I guess making API calls with inline c-like functions is the
meanest-leanest way to go short of assembly.
 
T

ThazKool

Ian said:
ThazKool wrote:

Please don't top post, it's considered rude in these parts.

I don't think any compiler would do this, it isn't the compiler's job.

That's why the question is OT here, you should ask on a platform
specific group, the platform's tools will look after linking.


I understand your concern. However, I am looking to draw on the wisdom
of the C++ community and not waste time looking at every
implementation. Thanks for you help.
 
P

Phlip

ThazKool said:
I understand your concern. However, I am looking to draw on the wisdom of
the C++ community and not waste time looking at every implementation.

I'm worried you are prematurely optimizing. Hard drive space is cheaper
than air these days. Unless you have a huge program or a tiny embedded
space, you should focus on design techniques that help your team rapidly
develop robust code, without worrying about the size of executable images.
They will be big, and our industry generally just lives with this.
 

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
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top