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

Are unused class methods removed from exe?

P: n/a
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.

Jun 16 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
ThazKool wrote:
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.
Jun 16 '06 #2

P: n/a
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++.

red floyd wrote:
ThazKool wrote:
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.


Jun 16 '06 #3

P: n/a
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.
ThazKool wrote:
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

Jun 16 '06 #4

P: n/a
ThazKool wrote:

Please don't top post, it's considered rude in these parts.
ThazKool wrote:
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.


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.

--
Ian Collins.
Jun 17 '06 #5

P: n/a
In article <1150497951.701731.267900
@i40g2000cwc.googlegroups.com>, Ch**********@gmail.com
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 17 '06 #6

P: n/a
ThazKool wrote:
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).

Jun 17 '06 #7

P: n/a
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.

Kaz Kylheku wrote:
ThazKool wrote:
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).


Jun 17 '06 #8

P: n/a

Ian Collins wrote:
ThazKool wrote:

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

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.


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.

--
Ian Collins.

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.

Jun 17 '06 #9

P: n/a
ThazKool wrote:
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.

--
Phlip

Jun 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.