我有一个应用程序,它使用 .so 共享库中的 Foo 类。我遇到了一个问题,它在运行时打印
<appname>: symbol lookup error: <appname>: undefined symbol: <mangled_Foo_symbol_name>
现在,事实证明未损坏的符号是针对类 Foo 的构造函数的,问题只是加载了一个旧版本的库,其中还没有包含 Foo。
我的问题不是解决错误(这显然是为了使用正确的库),而是为什么它出现在运行时而不是加载/启动时。
导致错误的代码行只是实例化了类 Foo 的对象,所以我在这里没有使用任何类似 dlopen 的东西,至少没有明确地/据我所知。
相比之下,如果我从加载搜索路径中删除整个库,我会在启动时收到此错误:
<appname>: error while loading shared libraries: libname.so.2: cannot open shared object file: No such file or directory
当加载路径上的gcc/libstdc++版本错误时,启动时也会出现错误:
<appname>: /path/to/gcc-4.8.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by <appname>)
这种“快速失败”行为更可取,我不想先运行我的应用程序很长一段时间,直到我最终意识到它使用了错误的库。 是什么导致加载错误在运行时出现,我怎样才能让它立即出现?
最佳答案
来自 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. 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.
关于c++ - 运行时而不是加载时出现符号查找错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32903870/