简体   繁体   English

链接器是否可以报告未使用的成员函数? (C ++)(GCC)

[英]Can unused member functions be reported by the linker? (C++)(gcc)

std::string has over 30 member functions that can be called on a string object. std :: string有30多个可以在字符串对象上调用的成员函数。
What if I only use a few of them? 如果我只使用其中一些怎么办?

I assume the unused member functions will not take up space in the executable code section. 我假设未使用的成员函数不会占用可执行代码部分中的空间。
I'm curious to know whether or not it's possible for the linker to determine a member function unused, remove it from being part of the compiled binary, and report what functions it threw away. 我很想知道链接器是否有可能确定一个未使用的成员函数,将其从编译后的二进制文件中删除,并报告它丢弃了哪些函数
Is there any way to do this? 有没有办法做到这一点? I was looking at gcc's linker flags , but I couldn't find anything relevant. 我在看gcc的链接器标志 ,但我找不到任何相关的东西。

Since std::string is a template class ( std::string is only a typedef to std::basic_string<char> ), only the used methods will be instantiated, so no unused methods will ever be compiled and so they can't be removed from your executable. 由于std::string是一个模板类( std::string只是std::basic_string<char>一个typedef ),所以只会实例化使用过的方法,因此不会编译任何未使用的方法,因此它们不能从您的可执行文件中删除。

Regarding non-template classes: virtual functions will always end up in the executable, regardless whether they are called, because there address is needed for the vtable. 关于非模板类: virtual函数总是以可执行文件结尾,无论它们是否被调用,因为vtable需要地址。 Other methods (as well as free functions), coming from the source of the executable or statically linked libraries, will only be linked into the binary if they are actually used. 来自可执行文件源或静态链接库的其他方法(以及自由函数)只有在实际使用时才会链接到二进制文件中。 But I know of no linker flag to print the functions which have not been linked in. 但我知道没有链接器标志来打印尚未链接的函数。

A shared library (.so) on the other hand, has to include all (exported) functions and methods, because a binary using this shared library could use any (exported) function. 另一方面,共享库(.so)必须包括所有(导出的)函数和方法,因为使用此共享库的二进制文件可以使用任何(导出的)函数。 But since a shared library can be used by many executables while being loaded into memory only once, this is usually worth it. 但是,由于共享库可以被许多可执行文件使用,而只被加载到内存中一次,这通常是值得的。

The standard library is normally shared, so it does not take any space in your executable. 标准库通常是共享的,因此它不会占用可执行文件中的任何空间。

If you link statically, then, as far as the linker is concerned, non-virtual member functions are just your regular garden variety functions with funny names. 如果您静态链接,那么,就链接器而言,非虚拟成员函数只是具有有趣名称的常规花园多样性函数。 Whatever the linker can do with normal functions, it can do with non-virtual members. 无论链接器可以对普通函数执行什么操作,它都可以与非虚拟成员一起使用。 I think GNU LD may be able to remove unused functions on certain architectures but not on others. 我认为GNU LD可能能够删除某些体系结构上未使用的函数,但不能删除其他体系结构。

Of course, function templates, such as the members of std::string are a different story altogether. 当然,函数模板,例如std::string的成员是完全不同的故事。 To the linker, they don't come from the library at all, but from your objects (and only those you did instantiate). 对于链接器,它们根本不是来自库,而是来自您的对象(只有那些您实例化的对象)。

这个答案并不完全涵盖你的情况,但如果你想知道你的类是否有任何未调用的函数,一些静态分析工具如cppcheck会报告这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM