Ruby 扩展链接错误
每当我尝试链接 我的 Ruby 扩展 时,我总是收到这个相当模糊的链接错误:
/usr/bin/ld: Mg.o: relocation R_X86_64_PC32 against undefined symbol `init_window_class_under' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
我找不到任何东西关于这一点。我试验了一段时间,当我删除头文件时,它链接得很好,所以我在没有它们的情况下继续前进(是的,非常糟糕的主意)。
事实证明我现在需要它们。那么,这个错误到底是什么,如何消除它呢?
更新:清除所有内容后,我开始收到这些警告:
warning: ‘init_window_class_under’ used but never defined
warning: ‘init_display_mode_class_under’ used but never defined
当我第一次遇到问题时,这些警告也会出现。我不太确定它们的意思。
I keep getting this fairly obscure link error whenever I try to link my Ruby extension:
/usr/bin/ld: Mg.o: relocation R_X86_64_PC32 against undefined symbol `init_window_class_under' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
I couldn't find anything on this. I experimented for a while and it linked fine when I removed the header files, so I moved on without them (Yes, very bad idea).
Turns out I need them, now. So, what is this error exactly, and how do I eliminate it?
Update: After clearing everything, I started getting these warnings:
warning: ‘init_window_class_under’ used but never defined
warning: ‘init_display_mode_class_under’ used but never defined
These also appeared when I first encountered the problem. I'm not exactly sure about what they mean.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新后的错误告诉您,您正在某处引用
init_window_class_under
和init_display_mode_class_under
但它们尚未定义。这些函数实际上是在Window.c
中定义的,但它们在源文件和头文件中都声明为static
。从Window.c
中的函数中删除static
链接修饰符,并在Window.h
中将它们声明为extern
。看来您在Display.c
以及x11
子目录中的所有内容中犯了同样的错误。任何声明为
static
的内容都具有文件作用域,并且在文件本身之外不可见。您原来的错误:
发生是因为
Window.c
中的所有函数(特别是init_window_class_under
)都是static
和static
函数不会产生任何可供链接器查找的符号。只有具有外部链接的实体才会在目标文件中产生符号。Your updated error is telling you that you're referencing
init_window_class_under
andinit_display_mode_class_under
somewhere but they are not defined. Those functions are actually defined inWindow.c
but they're declaredstatic
in both the source file and header file. Remove thestatic
linkage modifiers from the functions inWindow.c
and declare them asextern
inWindow.h
. Looks like you're making the same mistake inDisplay.c
and everything in thex11
subdirectory.Anything declared as
static
has file scope and is not visible outside the file itself.Your original error:
occurs because all the functions in
Window.c
(andinit_window_class_under
in particular) arestatic
andstatic
functions do not result in any symbols for the linker to find. Only entities with external linkage result in symbols in the object files.正如错误消息所暗示的那样,目标文件必须使用
-fPIC
构建才能链接到 x86-64 上的共享库(这在其他平台上也是一个好主意)。将
-fPIC
添加到您的CFLAGS
并重建所有对象。As the error message implies, object files must be built with
-fPIC
to be linked into shared libraries on x86-64 (it's a good idea on other platforms, too).Add
-fPIC
to yourCFLAGS
and rebuild all objects.