简体   繁体   English

运行时符号查找错误而不是加载时间

[英]Symbol lookup error at runtime instead of load time

I have an application which uses a class Foo from an .so shared library. 我有一个应用程序,它使用.so共享库中的类Foo。 I've come across a problem where at runtime it prints 我遇到了一个问题,在运行时它会打印出来

<appname>: symbol lookup error: <appname>: undefined symbol: <mangled_Foo_symbol_name> <appname>:符号查找错误:<appname>:未定义的符号:<mangled_Foo_symbol_name>

Now, it turned out that the unmangled symbol was for the constructor of the class Foo, and the problem was simply that an old version of the library was loaded, which didn't contain Foo yet. 现在,事实证明,unmangled符号是Foo类的构造函数,问题只是加载了一个旧版本的库,它还没有包含Foo。

My question isn't about resolving the error (that's obviously to use the correct library), but why it appears at runtime instead of at time of load / startup. 我的问题不是解决错误(显然是使用正确的库),而是为什么它出现在运行时而不是在加载/启动时。

The line of code causing the error just instantiates an object of class Foo, so I'm not using anything like dlopen here, at least not explicitly / to my knowledge. 导致错误的代码行只是实例化了一个类Foo的对象,所以我在这里没有使用像dlopen这样的东西,至少没有明确/我的知识。

In contrast, if I remove the whole library from the load search path, I get this error at startup : 相反,如果我从加载搜索路径中删除整个库,我在启动时会收到此错误:

<appname>: error while loading shared libraries: libname.so.2: cannot open shared object file: No such file or directory <appname>:加载共享库时出错:libname.so.2:无法打开共享对象文件:没有这样的文件或目录

When the wrong version of gcc / libstdc++ is on the load path, an error also appears at starup: 当错误版本的gcc / libstdc ++在加载路径上时,starup上也会出现错误:

<appname>: /path/to/gcc-4.8.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by <appname>) <appname>:/ path/to/gcc-4.8.0/lib64/libstdc++.so.6:找不到版本`GLIBCXX_3.4.20'(<appname>需要)


This "fail fast" behavior is much more desirable, I don't want to run my application for quite awhile first, until I finally realize it's using the wrong library. 这种“快速失败”行为更令人满意,我不想先运行我的应用程序很长一段时间,直到我终于意识到它使用了错误的库。 What causes the load error to appear at runtime and how can I make it appear immediately? 导致加载错误在运行时出现的原因是什么?如何立即显示?

From the man page of ld.so : ld.so的手册页:

ENVIRONMENT 环境

  • LD_BIND_NOW (libc5; glibc since 2.1.1) If set to a nonempty string, causes the dynamic linker to resolve all symbols at program startup instead of deferring function call resolution to the point when they are first referenced. LD_BIND_NOW (libc5;自2.1.1开始的glibc)如果设置为非空字符串,则会导致动态链接器在程序启动时解析所有符号,而不是将函数调用解析延迟到首次引用它们时。 This is useful when using a debugger. 这在使用调试器时很有用。
  • LD_WARN (ELF only)(glibc since 2.1.3) If set to a nonempty string, warn about unresolved symbols. LD_WARN (仅限ELF)(自2.1.3以来的glibc)如果设置LD_WARN空字符串,则警告未解析的符号。

I think you can not statically link .so library. 我想你不能静态链接.so库。 If you want to avoid load/run time errors you have to use all static libraries ( .a ). 如果要避免加载/运行时错误,则必须使用所有静态库( .a )。 If you do not have static version of library and source then try to find some statifier . 如果你没有静态版本的库和源,那么试着找一些statifier After googling I find few statifiers but do not know how do they work so leaving that part up to you. 谷歌搜索后,我发现很少有statifiers,但不知道它们是如何工作的,所以留下那部分给你。

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

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