-
[root@localhost dynamic]# ls -al
-
total 40
-
drwxr-xr-x 2 root root 4096 Jul 17 18:25 .
-
drwxr-xr-x 4 root root 4096 Jul 16 16:51 ..
-
-rw-r--r-- 1 root root 63 Jul 17 18:25 add.c
-
-rw-r--r-- 1 root root 66 Jul 17 18:25 add.h
-
-rw-r--r-- 1 root root 143 Jul 17 18:25 main.c
-
[root@localhost dynamic]# gcc -fPIC -Wall -c add.c -o add.o
-
[root@localhost dynamic]# gcc -shared -o libadd.so add.o
-
--share: 指定生成动态链接库
-
-fPIC: 编译为位置独立的代码,用于编译共享库。目标文件需要创建成位置无关的代码,概念上就是在可执行程序转载它们的时候,它们可以放在可执行程序的内存里的任何地方
-
[root@localhost dynamic]# gcc main.c -ladd -L. -o main
-
[root@localhost dynamic]# ls -al
-
total 72
-
drwxr-xr-x 2 root root 4096 Jul 17 18:28 .
-
drwxr-xr-x 4 root root 4096 Jul 16 16:51 ..
-
-rw-r--r-- 1 root root 63 Jul 17 18:25 add.c
-
-rw-r--r-- 1 root root 66 Jul 17 18:25 add.h
-
-rw-r--r-- 1 root root 1240 Jul 17 18:26 add.o
-
-rwxr-xr-x 1 root root 5591 Jul 17 18:27 libadd.so
-
-rwxr-xr-x 1 root root 7140 Jul 17 18:28 main
-
-rw-r--r-- 1 root root 143 Jul 17 18:25 main.c
-
[root@localhost dynamic]# ./main
-
./main: error while loading shared libraries: libadd.so: cannot open shared object file: No such file or directory
找不到链接库
解决方法:
1.将libadd.so 拷贝到/usr/lib 或者/lib中,然后调用ldconfig
2.export LD_LIBRARY_PATH=$(pwd)
-
[root@localhost dynamic]# cp libadd.so /usr/lib
-
[root@localhost dynamic]# ./main
-
./main: error while loading shared libraries: libadd.so: cannot open shared object file: No such file or directory
-
[root@localhost c_language]# ldconfig
[root@localhost c_language]# cd lib/dynamic/
[root@localhost dynamic]# ./main
1 + 2 = 3
阅读(797) | 评论(0) | 转发(0) |