当前位置 : 主页 > 编程语言 > java >

动态链接库嵌套样例

来源:互联网 收集:自由互联 发布时间:2022-06-23
foo2.c int foo2 = 20 ; void foo2_func () {} foo1.c extern int foo2 ; int foo1 = 10 ; int dummy = 20 ; void foo1_func () { int a = foo2 ; int b = dummy ; int c = foo1 ; foo2_func (); } main.c #include stdlib.h extern int foo1 ; extern int fo

foo2.c

int foo2 = 20;
void foo2_func() {}

foo1.c

extern int foo2;
int foo1 = 10;
int dummy = 20;

void foo1_func()
{
int a = foo2;
int b = dummy;
int c = foo1;

foo2_func();
}

main.c

#include <stdlib.h>
extern int foo1;
extern int foo1;
int a[2048];
int dummy = 10;

void main()
{
char *m = malloc(1024);
foo1 = 5;

foo1_func();
while(1)sleep(1000);
}

运行j结果:

jiaming@jiaming-pc:~/Documents/test/libso$ gcc -shared -fPIC -o libf2.so foo2.c
jiaming@jiaming-pc:~/Documents/test/libso$ gcc -shared -fPIC -o libf1.so foo1.c -L. -lf2
foo1.c: In function ‘foo1_func’:
foo1.c:11:5: warning: implicit declaration of function ‘foo2_func’; did you mean ‘foo1_func’? [-Wimplicit-function-declaration]
foo2_func();
^~~~~~~~~
foo1_func
jiaming@jiaming-pc:~/Documents/test/libso$ gcc -o hello main.c -L. -lf1
main.c: In function ‘main’:
main.c:12:5: warning: implicit declaration of function ‘foo1_func’ [-Wimplicit-function-declaration]
foo1_func();
^~~~~~~~~
main.c:13:13: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
while(1)sleep(1000);
^~~~~
/usr/bin/ld: warning: libf2.so, needed by ./libf1.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: ./libf1.so: undefined reference to `foo2'
/usr/bin/ld: ./libf1.so: undefined reference to `foo2_func'
collect2: error: ld returned 1 exit status
jiaming@jiaming-pc:~/Documents/test/libso$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
jiaming@jiaming-pc:~/Documents/test/libso$ gcc -o hello main.c -L. -lf1
main.c: In function ‘main’:
main.c:12:5: warning: implicit declaration of function ‘foo1_func’ [-Wimplicit-function-declaration]
foo1_func();
^~~~~~~~~
main.c:13:13: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
while(1)sleep(1000);
^~~~~
jiaming@jiaming-pc:~/Documents/test/libso$ ./hello
^C
  • 动态库在制作的时候,亦可结合其它动态库。
  • -I(注意是大写的i)给gcc添加自定义的头文件的路径,​​-Imyinclude​​。
  • -L 给gcc添加额外的搜索库的路径。
  • -l (小写L)参数就是用来指定程序要链接的库,-l参数紧接着就是库名,数学库文件名是libm.so,很容易看出,把库文件名的头lib和尾.so去掉就是库名了,​​lm​​。

  • 上一篇:端午安康,使用祝福话语生成词云吧
    下一篇:没有了
    网友评论