链接 C++ 时惰性符号绑定失败动态库
我正在用 C++ 编写一个 dylib,但是当我尝试将其链接到我的应用程序时,它在执行时给我一个错误:
dyld: lazy symbol binding failed: Symbol not found: __ZN8Vector2DC1Ev
Referenced from: /Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug/Singularity Test App
Expected in: /Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug/libSingularity.dylib
dyld: Symbol not found: __ZN8Vector2DC1Ev
Referenced from: /Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug/Singularity Test App
Expected in: /Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug/libSingularity.dylib
我确保符号没有被剥离,并
nm -g libSingularity.dylib | grep "T" | grep __ZN8Vector
报告该符号存在于库中:
0000000000006df6 T __ZN8Vector2DC1Eff
0000000000006d98 T __ZN8Vector2DC1Ev
0000000000006dc2 T __ZN8Vector2DC2Eff
0000000000006d6e T __ZN8Vector2DC2Ev
I我是从 XCode 内部链接的,所以链接命令有点奇怪。 这是 dylib 的链接器命令:
Ld "/Users/noahz/Desktop/Singularity/Singularity Engine/build/Debug
/libSingularity.dylib" normal x86_64
cd "/Users/noahz/Desktop/Singularity/Singularity Engine"
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -dynamiclib -isysroot /Develope
/SDKs/MacOSX10.6.sdk "-L/Users/noahz/Desktop/Singularity/Singularity Engine/build
/Debug" "-F/Users/noahz/Desktop/Singularity/Singularity Engine/build/Debug"
-filelist "/Users/noahz/Desktop/Singularity/Singularity Engine/build/Singularity
Engine.build/Debug/Singularity Engine.build/Objects-normal/x86_64
/libSingularity.LinkFileList" -install_name libSingularity.dylib -mmacosx-version-
min=10.6 -framework sfml-system-d -framework sfml-window-d -framework SFML
-framework OpenGL -framework OpenAL -framework sfml-graphics-d -single_module
-compatibility_version 1 -current_version 1 -o "/Users/noahz/Desktop/Singularity
/Singularity Engine/build/Debug/libSingularity.dylib"
这是测试应用程序的链接器命令:
cd "/Users/noahz/Desktop/Singularity/Singularity Test App"
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk
"-L/Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug" "-F/Users
/noahz/Desktop/Singularity/Singularity Test App/build/Debug" -filelist "/Users/noahz
/Desktop/Singularity/Singularity Test App/build/Singularity Test App.build/Debug
/Singularity Test App.build/Objects-normal/x86_64/Singularity Test
App.LinkFileList" -mmacosx-version-min=10.6 "/Users/noahz/Desktop/Singularity
/Singularity Engine/build/Debug/libSingularity.dylib" -o "/Users/noahz/Desktop
/Singularity/Singularity Test App/build/Debug/Singularity Test App"
关于为什么会发生这种情况和/或如何修复它有什么想法吗?
I'm writing a dylib in C++, but when I try to link it into my application, it gives me an error on execution:
dyld: lazy symbol binding failed: Symbol not found: __ZN8Vector2DC1Ev
Referenced from: /Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug/Singularity Test App
Expected in: /Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug/libSingularity.dylib
dyld: Symbol not found: __ZN8Vector2DC1Ev
Referenced from: /Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug/Singularity Test App
Expected in: /Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug/libSingularity.dylib
I made sure the symbol wasn't stripped, and
nm -g libSingularity.dylib | grep "T" | grep __ZN8Vector
reports that the symbol is present in the library:
0000000000006df6 T __ZN8Vector2DC1Eff
0000000000006d98 T __ZN8Vector2DC1Ev
0000000000006dc2 T __ZN8Vector2DC2Eff
0000000000006d6e T __ZN8Vector2DC2Ev
I'm linking from inside XCode, so the link commands are kind of weird.
Here's the linker command for the dylib:
Ld "/Users/noahz/Desktop/Singularity/Singularity Engine/build/Debug
/libSingularity.dylib" normal x86_64
cd "/Users/noahz/Desktop/Singularity/Singularity Engine"
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -dynamiclib -isysroot /Develope
/SDKs/MacOSX10.6.sdk "-L/Users/noahz/Desktop/Singularity/Singularity Engine/build
/Debug" "-F/Users/noahz/Desktop/Singularity/Singularity Engine/build/Debug"
-filelist "/Users/noahz/Desktop/Singularity/Singularity Engine/build/Singularity
Engine.build/Debug/Singularity Engine.build/Objects-normal/x86_64
/libSingularity.LinkFileList" -install_name libSingularity.dylib -mmacosx-version-
min=10.6 -framework sfml-system-d -framework sfml-window-d -framework SFML
-framework OpenGL -framework OpenAL -framework sfml-graphics-d -single_module
-compatibility_version 1 -current_version 1 -o "/Users/noahz/Desktop/Singularity
/Singularity Engine/build/Debug/libSingularity.dylib"
and here's the linker command for the test app:
cd "/Users/noahz/Desktop/Singularity/Singularity Test App"
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk
"-L/Users/noahz/Desktop/Singularity/Singularity Test App/build/Debug" "-F/Users
/noahz/Desktop/Singularity/Singularity Test App/build/Debug" -filelist "/Users/noahz
/Desktop/Singularity/Singularity Test App/build/Singularity Test App.build/Debug
/Singularity Test App.build/Objects-normal/x86_64/Singularity Test
App.LinkFileList" -mmacosx-version-min=10.6 "/Users/noahz/Desktop/Singularity
/Singularity Engine/build/Debug/libSingularity.dylib" -o "/Users/noahz/Desktop
/Singularity/Singularity Test App/build/Debug/Singularity Test App"
Any ideas on why this is happening and/or how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的应用程序的架构 x86/64 与您尝试加载的库不同,这可能可以解释您遇到的问题。
两者必须使用相同的体系结构进行编译。
If your application differs in architecture x86/64 from the library you are trying to load, that might explain the problem you're having.
Both must be compiled with the same architecture.
我在链接阶段没有看到一行
显然加载动态库的另一种方法是从内部显式加载它
代码。
我只是解释一下我找到的一篇博客文章。
http://root.cern.ch /phpBB3//viewtopic.php?f=3&t=10380&start=0
I don't see a line in your link stage
apparently another way to load a dynamic library is to explicity load it from within
the code.
I'm just paraphrasing a blog article I found.
http://root.cern.ch/phpBB3//viewtopic.php?f=3&t=10380&start=0
我通过静态链接库解决了这个问题。它不像动态链接那么优雅,但至少它不会反复崩溃。
I solved the problem by linking the library statically. It's not as elegant as dynamic linking was, but at least it doesn't crash repeatedly.