- int factorial( int n)
- {
- if(n <= 1)
- {
- return 1;
- }
- else
- {
- return n*factorial(n-1);
- }
- }
- int cmp(int a,int b)
- {
- return a-b;
- }
- root@libin:~/program/C/testlib/lib# ll
- 总用量 16
- drwxr-xr-x 2 root root 4096 2012-07-27 20:08 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- root@libin:~/program/C/testlib/lib# gcc -c *.c
- root@libin:~/program/C/testlib/lib# ll
- 总用量 24
- drwxr-xr-x 2 root root 4096 2012-07-27 20:08 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 679 2012-07-27 20:08 cmp.o
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- -rw-r--r-- 1 root root 764 2012-07-27 20:08 factorial.o
- root@libin:~/program/C/testlib/lib# ar rs libmymath.a *.o
- ar: creating libmymath.a
- root@libin:~/program/C/testlib/lib# ll
- 总用量 28
- drwxr-xr-x 2 root root 4096 2012-07-27 20:19 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 679 2012-07-27 20:08 cmp.o
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- -rw-r--r-- 1 root root 764 2012-07-27 20:08 factorial.o
- -rw-r--r-- 1 root root 1658 2012-07-27 20:19 libmymath.a
- root@libin:~/program/C/testlib/lib# ar tv libmymath.a
- rw-r--r-- 0/0 679 Jul 27 20:08 2012 cmp.o
- rw-r--r-- 0/0 764 Jul 27 20:08 2012 factorial.o
- root@libin:~/program/C/testlib/lib# rm *.o
- root@libin:~/program/C/testlib/lib# ll
- 总用量 20
- drwxr-xr-x 2 root root 4096 2012-07-27 20:24 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- -rw-r--r-- 1 root root 1658 2012-07-27 20:19 libmymath.a
- root@libin:~/program/C/testlib/lib# ar x libmymath.a
- root@libin:~/program/C/testlib/lib# ll
- 总用量 28
- drwxr-xr-x 2 root root 4096 2012-07-27 20:42 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 679 2012-07-27 20:42 cmp.o
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- -rw-r--r-- 1 root root 764 2012-07-27 20:42 factorial.o
- -rw-r--r-- 1 root root 1658 2012-07-27 20:19 libmymath.a
- root@libin:~/program/C/testlib/lib# ar xo libmymath.a
- root@libin:~/program/C/testlib/lib# ll
- 总用量 28
- drwxr-xr-x 2 root root 4096 2012-07-27 20:42 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 679 2012-07-27 20:08 cmp.o
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- -rw-r--r-- 1 root root 764 2012-07-27 20:08 factorial.o
- -rw-r--r-- 1 root root 1658 2012-07-27 20:19 libmymath.a
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc ,char* argv[])
- {
- int a = 4;
- int b = 5;
- int f_a = factorial(a);
- int f_b = factorial(b);
- while(1)
- {
- if(cmp(f_a,f_b))
- {
- printf("f_a is bigger than f_b\n");
- }
- else
- {
- printf("f_a is not bigger than f_b\n");
- }
- sleep(100);
- }
- return 0;
- }
- root@libin:~/program/C/testlib/use# gcc -o test test.c
- /tmp/ccZM4LMy.o: In function `main':
- test.c:(.text+0x21): undefined reference to `factorial'
- test.c:(.text+0x31): undefined reference to `factorial'
- test.c:(.text+0x49): undefined reference to `cmp'
- collect2: ld returned 1 exit status
- root@libin:~/program/C/testlib/use# gcc -o test test.c -L ../lib/ -lmymath
- root@libin:~/program/C/testlib/use# ll
- 总用量 20
- drwxr-xr-x 2 root root 4096 2012-07-27 21:01 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rwxr-xr-x 1 root root 7307 2012-07-27 21:01 test*
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# gcc -o test test.c -lmymath
- /usr/bin/ld: cannot find -lmymath
- collect2: ld returned 1 exit status
- root@libin:~/program/C/testlib/use#
- root@libin:~/program/C/testlib/use# rm test
- root@libin:~/program/C/testlib/use# ll
- 总用量 12
- drwxr-xr-x 2 root root 4096 2012-07-27 21:29 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# gcc -o test test.c ../lib/cmp.o ../lib/factorial.o
- root@libin:~/program/C/testlib/use# ll
- 总用量 20
- drwxr-xr-x 2 root root 4096 2012-07-27 21:29 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rwxr-xr-x 1 root root 7307 2012-07-27 21:29 test*
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# gcc -o test test.c -L ../lib/ -lmymath
- root@libin:~/program/C/testlib/use# ll
- 总用量 20
- drwxr-xr-x 2 root root 4096 2012-07-27 21:36 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rwxr-xr-x 1 root root 7307 2012-07-27 21:36 test*
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# rm ../lib/libmymath.a
- root@libin:~/program/C/testlib/use# ./test
- f_a is bigger than f_b
- root@libin:~/program/C/testlib/lib# ll
- 总用量 16
- drwxr-xr-x 2 root root 4096 2012-07-27 21:44 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- root@libin:~/program/C/testlib/lib# gcc -shared -o libmymath.so *.c
- root@libin:~/program/C/testlib/lib# ll
- 总用量 24
- drwxr-xr-x 2 root root 4096 2012-07-27 21:44 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- -rwxr-xr-x 1 root root 6688 2012-07-27 21:44 libmymath.so*
- root@libin:~/program/C/testlib/use# ll
- 总用量 12
- drwxr-xr-x 2 root root 4096 2012-07-27 21:57 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# gcc -o test test.c -L ../lib/ -lmymath
- root@libin:~/program/C/testlib/use# ll
- 总用量 20
- drwxr-xr-x 2 root root 4096 2012-07-27 21:57 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rwxr-xr-x 1 root root 7265 2012-07-27 21:57 test*
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# ./test
- ./test: error while loading shared libraries: libmymath.so: cannot open shared object file: No such file or directory
- root@libin:~/program/C/testlib/use# objdump -d test
- test: file format elf32-i38
- ....
- 08048564 <main>:
- 8048564: 55 push %ebp
- 8048565: 89 e5 mov %esp,%ebp
- 8048567: 83 e4 f0 and $0xfffffff0,%esp
- 804856a: 83 ec 20 sub $0x20,%esp
- 804856d: c7 44 24 1c 04 00 00 movl $0x4,0x1c(%esp)
- 8048574: 00
- 8048575: c7 44 24 18 05 00 00 movl $0x5,0x18(%esp)
- 804857c: 00
- 804857d: 8b 44 24 1c mov 0x1c(%esp),%eax
- 8048581: 89 04 24 mov %eax,(%esp)
- 8048584: e8 f3 fe ff ff call 804847c <factorial@plt>
- 8048589: 89 44 24 14 mov %eax,0x14(%esp)
- 804858d: 8b 44 24 18 mov 0x18(%esp),%eax
- 8048591: 89 04 24 mov %eax,(%esp)
- 8048594: e8 e3 fe ff ff call 804847c <factorial@plt>
- 8048599: 89 44 24 10 mov %eax,0x10(%esp)
- 804859d: 8b 44 24 10 mov 0x10(%esp),%eax
- 80485a1: 89 44 24 04 mov %eax,0x4(%esp)
- 80485a5: 8b 44 24 14 mov 0x14(%esp),%eax
- 80485a9: 89 04 24 mov %eax,(%esp)
- 80485ac: e8 bb fe ff ff call 804846c <cmp@plt>
- 80485b1: 85 c0 test %eax,%eax
- 80485b3: 74 0e je 80485c3 <main+0x5f>
- 80485b5: c7 04 24 a0 86 04 08 movl $0x80486a0,(%esp)
- 80485bc: e8 db fe ff ff call 804849c <puts@plt>
- 80485c1: eb 0c jmp 80485cf <main+0x6b>
- 80485c3: c7 04 24 b7 86 04 08 movl $0x80486b7,(%esp)
- 80485ca: e8 cd fe ff ff call 804849c <puts@plt>
- 80485cf: c7 04 24 64 00 00 00 movl $0x64,(%esp)
- 80485d6: e8 b1 fe ff ff call 804848c <sleep@plt>
- 80485db: eb c0 jmp 804859d <main+0x39>
- 80485dd: 90 nop
- 80485de: 90 nop
- 80485df: 90 nop
- ....
- root@libin:~/program/C/testlib/use# mv ../lib/libmymath.so /usr/lib/
- root@libin:~/program/C/testlib/use# ./test
- f_a is bigger than f_b
- root@libin:~/program/C/testlib/use# cat /etc/ld.so.conf
- include /etc/ld.so.conf.d/*.conf
- /home/libin/program/C/testlib/lib/
- root@libin:~/program/C/testlib/use# mv /usr/lib/libmymath.so ../lib/
- root@libin:~/program/C/testlib/use# ll ../lib/
- cmp.c factorial.c libmymath.so
- root@libin:~/program/C/testlib/use# ll ../lib/
- 总用量 24
- drwxr-xr-x 2 root root 4096 2012-07-27 22:25 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 41 2012-07-26 22:50 cmp.c
- -rw-r--r-- 1 root root 115 2012-07-27 20:07 factorial.c
- -rwxr-xr-x 1 root root 6688 2012-07-27 21:44 libmymath.so*
- root@libin:~/program/C/testlib/use# ll
- 总用量 20
- drwxr-xr-x 2 root root 4096 2012-07-27 22:05 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rwxr-xr-x 1 root root 7265 2012-07-27 21:57 test*
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# ll
- 总用量 20
- drwxr-xr-x 2 root root 4096 2012-07-27 22:05 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rwxr-xr-x 1 root root 7265 2012-07-27 21:57 test*
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# ./test
- ./test: error while loading shared libraries: libmymath.so: cannot open shared object file: No such file or directory
- root@libin:~/program/C/testlib/use# ldconfig
- root@libin:~/program/C/testlib/use# ./test
- f_a is bigger than f_b
- root@libin:~/program/C/testlib/use# rm test
- root@libin:~/program/C/testlib/use# ll
- 总用量 12
- drwxr-xr-x 2 root root 4096 2012-07-28 00:30 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# gcc -o test test.c -L ../lib/ -lmymath -Wl,-rpath, ../lib/libmymath.so
- root@libin:~/program/C/testlib/use# ll
- 总用量 20
- drwxr-xr-x 2 root root 4096 2012-07-28 00:30 ./
- drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
- -rwxr-xr-x 1 root root 7265 2012-07-28 00:30 test*
- -rw-r--r-- 1 root root 405 2012-07-26 23:12 test.c
- root@libin:~/program/C/testlib/use# ./test
- f_a is bigger than f_b
1.编译目标代码时指定的动态库搜索路径;//-wl.-rpath
2.环境变量LD_LIBRARY_PATH指定的动态库搜索路径;//不推荐使用
3.配置文件/etc/ld.so.conf中指定的动态库搜索路径;//本文提到了
4.默认的动态库搜索路径/lib;
5.默认的动态库搜索路径/usr/lib。
注:64位机器自动找lib64库
如何查看一个可执行文件需要那些动态库的支持呢?ldd命令
- root@libin:~/program/C/testlib/use# ldd test
- linux-gate.so.1 => (0x002dd000)
- libmymath.so => /home/libin/program/C/testlib/lib/libmymath.so (0x00d81000)
- libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00110000)
- /lib/ld-linux.so.2 (0x00f9e000)