屏蔽动态库之间的依赖

假定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)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值