linux动态库的生成与使用

Linux动态库的生成:

so_tets.h    so_a.c   so_b.c    so_c.c

生成动态库

gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so



编译测试主程序时的区别:

1、用gcc test.c  -ltest -o test:

需要把libtest.so拷贝到/usr/lib/目录下,然后执行ldconfig命令,否则运行时会出错。

但是我尚未明白以下问题:

在直接运行时,会提示出错:

./test: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory

通过ldd命令:

ldd test
	linux-vdso.so.1 =>  (0x00007ffedee6a000)
	libtest.so => not found
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007eff2d313000)
	/lib64/ld-linux-x86-64.so.2 (0x0000557e84157000)
这里提示没有找到这个依赖库。链接显然是没有问题的,找到了这个库。


我的猜想是,这里1的链接,链接的是/usr/lib目录下的库,而方法2则是链接绝对路径下的库


2、用gcc -o test test.c ./libtest.so

直接运行

猜你喜欢

转载自blog.csdn.net/k_wang_/article/details/71480252
今日推荐