Can unused member functions be reported by the linker? (C ++) (NCA)

std :: string has more than 30 member functions that can be called on a string object.
What if I use only some of them?

I assume that unused member functions do not take up space in the executable section.
I am curious to find out if it is possible for the linker to define a member function that is not used, remove it from part of the compiled binary, and tell which functions it throws.
Is there any way to do this? I looked at the gcc flags of the linker , but I could not find anything suitable.

+6
source share
3 answers

Since std::string is a template class ( std::string is only from typedef to std::basic_string<char> ), only used methods will be used, therefore no unused methods will ever be compiled, and therefore they cannot be removed from your executable file.

As for classes without a template: virtual functions will always be executed in an executable file, regardless of whether they are called, because vtable needs an address. Other methods (as well as free functions) coming from the source of executable or statically linked libraries will only be linked in binary format if they are actually used. But I do not know which linker flag to print functions that were not related.

On the other hand, a shared library (.so) should include all (exported) functions and methods, since binary code using this shared library can use any (exported) function. But since a shared library can be used by many executables only once during loading into memory, it is usually worth it.

+4
source

The standard library is usually shared, so it does not take up space in the executable file.

If you are linking statically, then as far as the linker is concerned, non-virtual member functions are just your usual garden variety functions with funny names. Regardless of what the linker can do with normal functions, it can work with non-virtual participants. I think GNU LD can remove unused functions on certain architectures, but not on others.

Of course, functional patterns, such as std::string members, are completely different. For the linker, they do not come from the library at all, but from your objects (and only those that you created).

0
source

This answer is not entirely relevant to your case, but if you want to find out if there are any unclaimed functions of YOUR classes, some static analysis tools like cppcheck will report this.

0
source

Source: https://habr.com/ru/post/916861/


All Articles