未使用功能的链接器错误:它们何时发生?

时间:2021-12-14 15:01:08

Assume a static library libfoo that depends on another static library libbar for some functionality. These and my application are written in D. If my application only uses libfoo directly, and only calls functions from libfoo that do not reference symbols from libbar, sometimes the program links successfully without passing libbar to the linker and other times it doesn't.

假设一个静态库libfoo依赖于另一个静态库libbar来实现某些功能。这些和我的应用程序都是用D编写的。如果我的应用程序只直接使用libfoo,并且只调用libfoo中不引用libbar符号的函数,有时程序链接成功而不将libbar传递给链接器,有时则不会。

Which of these happens seems to depend on what compiler I'm using to compile libfoo, libbar and my application, even though all compilers use the GCC toolchain to link. If I'm using DMD, I never receive linker errors if I don't pass libbar to the linker. If I'm using GDC, I sometimes do, for reasons I don't understand. If I'm using LDC, I always do.

发生了哪些似乎取决于我用来编译libfoo,libbar和我的应用程序的编译器,即使所有编译器都使用GCC工具链进行链接。如果我正在使用DMD,如果我没有将libbar传递给链接器,我永远不会收到链接器错误。如果我正在使用GDC,我有时会这样做,原因我不明白。如果我使用LDC,我总是这样做。

What determines whether the GCC linker fails when a symbol referred in libfoo is undefined, but this symbol occurs in a function not referred to by the application object file?

当libfoo中引用的符号未定义时,确定GCC链接器是否失败的原因是什么,但此符号出现在应用程序对象文件未引用的函数中?

2 个解决方案

#1


1  

What determines whether the GCC linker fails when a symbol referred in libfoo is undefined, but this symbol occurs in a function not referred to by the application object file?

当libfoo中引用的符号未定义时,确定GCC链接器是否失败的原因是什么,但此符号出现在应用程序对象文件未引用的函数中?

If the linker complains about an unresolved symbol, then that is symbol is referenced from somewhere.

如果链接器抱怨未解析的符号,则从某处引用该符号。

Usually the linker will tell you which object the unresolved reference comes from, but if it doesn't, the -Wl,-y,unres_symbol should.

通常链接器将告诉您未解析的引用来自哪个对象,但如果不是,则-Wl,-y,unres_symbol应该。

You may also want to read this description of how the whole thing works.

您可能还想阅读整个过程如何工作的描述。

#2


1  

if the linker does no effort to eliminate dead (unused) code in the libraries it simply assumes all referenced symbols are used and tries to link them in

如果链接器不努力消除库中的死(未使用)代码,它只是假设使用了所有引用的符号并尝试将它们链接在一起

if it does the elimination (through for example a simple mark and sweep algo (note that you cannot fully decide if some code is unused as that problem can be reduced to the halting problem)) it can eliminate the unused libraries if they are never used

如果它消除(通过例如简单的标记和扫描算法(请注意,您无法完全确定某些代码是否未被使用,因为该问题可以简化为暂停问题))如果它们从未使用过,它可以消除未使用的库

this behavior is implementation defined (and there may be linker flags you can set to en/disable it

此行为是实现定义的(并且可能存在可以设置为/禁用它的链接器标志

#1


1  

What determines whether the GCC linker fails when a symbol referred in libfoo is undefined, but this symbol occurs in a function not referred to by the application object file?

当libfoo中引用的符号未定义时,确定GCC链接器是否失败的原因是什么,但此符号出现在应用程序对象文件未引用的函数中?

If the linker complains about an unresolved symbol, then that is symbol is referenced from somewhere.

如果链接器抱怨未解析的符号,则从某处引用该符号。

Usually the linker will tell you which object the unresolved reference comes from, but if it doesn't, the -Wl,-y,unres_symbol should.

通常链接器将告诉您未解析的引用来自哪个对象,但如果不是,则-Wl,-y,unres_symbol应该。

You may also want to read this description of how the whole thing works.

您可能还想阅读整个过程如何工作的描述。

#2


1  

if the linker does no effort to eliminate dead (unused) code in the libraries it simply assumes all referenced symbols are used and tries to link them in

如果链接器不努力消除库中的死(未使用)代码,它只是假设使用了所有引用的符号并尝试将它们链接在一起

if it does the elimination (through for example a simple mark and sweep algo (note that you cannot fully decide if some code is unused as that problem can be reduced to the halting problem)) it can eliminate the unused libraries if they are never used

如果它消除(通过例如简单的标记和扫描算法(请注意,您无法完全确定某些代码是否未被使用,因为该问题可以简化为暂停问题))如果它们从未使用过,它可以消除未使用的库

this behavior is implementation defined (and there may be linker flags you can set to en/disable it

此行为是实现定义的(并且可能存在可以设置为/禁用它的链接器标志