我有以下文件:
文件 liba.c
#include <stdio.h>
void foo() {printf("foo\n"); }
void bar() {printf("bar\n"); }
文件 libb.c
void foo();
void abc() { foo(); }
主.c
void abc();
void foo();
void bar();
int main()
{
abc();
foo();
bar();
}
我编译的文件如下:
gcc -shared -fPIC -o liba.so liba.c
gcc -shared -fPIC -Wl,-rpath,. -Wl,--no-undefined -o libb.so libb.c liba.so
gcc -Wl,-rpath,. main.c libb.so
现在链接器在链接可执行文件时报告错误:
/usr/bin/ld: /tmp/cc4De1Xu.o: undefined reference to symbol 'bar'
./liba.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
现在的问题是:为什么链接器找不到符号bar? main.c 链接到 libb.so,后者又链接到 liba.so 和 liba.so 确实有符号 bar。 main.c 使用符号 foo 并且它也驻留在 liba.so 中并且在链接期间找到。
因此,foo 和 bar 这两个符号都在 liba.so 中定义,但只找到了 foo在链接期间。为什么?
看起来只有 liba.so 中的符号被发现在 libb.so 中使用。但为什么会这样呢?
最佳答案
我认为这与符号 bar
不包含在 libb.so 的导出表中但仅包含在 liba.so 中有关.为什么不将 main.c 也链接到 liba.so?
否则,在 libb.c 中声明 void bar();
应该会有所帮助..
关于c - 为什么 ld 没有从共享库中找到一些符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606612/