我遇到链接问题.我需要链接到共享库libfoo.so,该库依赖于函数read,我想在文件read.c中定义自己.
I'm having a linking problem. I need to link against a shared library libfoo.so that depends on a function read which I would like to define myself in the file read.c.
我将所有内容编译并链接在一起,但是在运行时出现错误
I compile and link everything together but at runtime I get the error
/home/bar/src/libfoo.so: undefined symbol: sread.nm报告符号已定义
$nm baz | grep sread 00000000000022f8 t sread但是ldd报告该符号未定义
but ldd reports the symbol is undefined
$ldd -r baz | grep sread undefined symbol: sread (/home/bar/src/libfoo.so)有什么作用? libfoo.so是共享库是否存在一些问题?
What gives? Is there some isse with the fact that libfoo.so is a shared library?
推荐答案首先,定义一个名为"read"的函数不是一个好主意,因为它是所有UNIXen上的标准libc函数.执行此操作时,程序的行为是不确定的.
First, defining a function called 'read' is a bad idea(TM), because it is a standard libc function on all UNIXen. The behavior of your program is undefined when you do this.
第二,在libbaz.so中定义的read函数在nm输出中标记为't'.这意味着此功能是本地功能(在libbaz.so外部不可见).全局功能由nm用'T'标记.
Second, the read function you defined in libbaz.so is marked with a 't' in nm output. This means that this function is local (not visible outside libbaz.so). Global functions are marked with 'T' by nm.
在read.c中定义'static int read(...)'时是否使用过? 如果没有,那么在编译并链接libbaz.so时是否在命令行中使用了链接脚本,attribute((visibility(hidden)))或-fvisibility=hidden?
Did you use 'static int read(...)' when you defined it in read.c? If not, did you use a linker script, or attribute((visibility(hidden))), or perhaps -fvisibility=hidden on command line when you compiled and linked libbaz.so?
更多推荐
nm报告符号已定义,但ldd报告符号未定义
发布评论