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
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.cjiaming@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