`

linux下动态库的编译使用

阅读更多
linux下动态库的编译

1 实践
test_cl.c:
  1 #include<stdio.h>
  2 #include <stddef.h>
  3
  4 extern int add(int a, int b);
  5 #define LabelAddr
  6 main()
  7 {
  8     int i = 0;
  9     printf("hello world!\n");
10     printf("label_abc=%d\n", &&label_abc);
11 //    i = &&label_abc;
12 //    goto *i;
13     goto *(&&label_abc);
14     printf("after goto\n");
15     label_abc:
16         printf("label_abc\n");
17        i++;
18
19
20     printf("add(a+b) = %d\n", add(1, 2));
21     return 0;
22
23 }


test_so.c
  1 int add(int a, int b)
  2 {
  3     return a + b;
  4 }


test_so1.c
  1 int add(int a, int b)
  2 {
  3     return a - b;
  4 }



1、
gcc -c -fPIC test_so.c
生成test_so.o
2、
gcc -shared -fPIC -o libtest_so.so test_so.o
生成libtest_so.so
3、
(rm test_so.o)
gcc test_cl.c -L. -ltest_so
4、
export LD_LIBRARY_PATH=`pwd`
5、
./a.out
结果是add(a+b) = 3
6、
(rm libtest_so.so)
gcc -shared -fPIC -o libtest_so.so test_so1.c
7、
./a.out
结果是add(a+b) = -1

(如果动态库依赖于其他的动态库不存在,在链接成可执行文件时才会报错)
(优先链接动态库的,除非用-static参数指定链接静态库)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics