假定A程序用到了动态库b, b又用到了动态库c, 那么在编译A的时候,需要在链接符号连指定c吗?
如果能不指定的话,是最好的。
$ cat hello.c
extern void foo(void);
void hello(void)
{
foo();
}
$ cat foo.c
void foo(void)
{
}
$ cat test.c
extern void hello(void);
int main(void)
{
hello();
return 0;
}
$ gcc -fpic -shared -g foo.c -o libfoo.so
gcc -fpic -shared hello.c -g -o libhello.so
$ gcc test.c -g -o test -L. -lhello -lfoo
$ gcc test.c -g -o test -L. -lhello
./libhello.so: undefined reference to `foo'
collect2: error: ld returned 1 exit status
从上面来看,不指定的话,会导致编译错误。
尝试在编译 hello的时候指定 foo:
$ gcc -fpic -shared hello.c -g -o libhello.so -lfoo -L.
$ gcc test.c -g -o test -L. -lhello
/usr/bin/ld: warning: libfoo.so, needed by ./libhello.so, not found (try using -rpath or -rpath-link)
./libhello.so: undefined reference to `foo'
collect2: error: ld returned 1 exit status
$ ldd libhello.so
linux-gate.so.1 => (0xb76dc000)
libfoo.so => not found
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb74fa000)
/lib/ld-linux.so.2 (0xb76dd000)
首选,使用 rpath来解决:
$ gcc -fpic -shared hello.c -g -o libhello.so libfoo.so -Wl,-rpath=.
$ gcc test.c -g -o test -lhello -L.
$ ldd libhello.so
linux-gate.so.1 => (0xb76ed000)
libfoo.so => ./libfoo.so (0xb76e3000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7508000)
/lib/ld-linux.so.2 (0xb76ee000)
链接问题没有了
如果不想加 -Wl,-rpath选项,可以把 libfoo.so放到 标准搜索路径下面:
$ gcc -fpic -shared hello.c -g -o libhello.so -L. -lfoo
$ sudo cp libfoo.so /lib/
$ gcc test.c -g -o test -lhello -L.
$ ldd libhello.so
linux-gate.so.1 => (0xb7732000)
libfoo.so => /lib/libfoo.so (0xb7703000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb754d000)
/lib/ld-linux.so.2 (0xb7733000)