有两个so,其中soB中调用soA;测试
那么咱们打包soB的时候链接soA;blog
在打包test程序的时候链接soB,此时soB会自动查找依赖的soA;io
以下测试编译
在编译以前指定环境变量:export LD_LIBRARY_PATH=./class
soAtest
#include <stdio.h> int add(int a,int b){ return (a+b); }
编译成so变量
gcc -shared -fPIC -o libadd.so add.c
soBgcc
#include <stdio.h> extern int add(int a,int b); int cal(int a,int b){ return (add(a,b)+1); }
编译成so,编译时链接soA,就是 -ladd打包
gcc -shared -fPIC -o libcal.so cal.c -L. -ladd
测试程序test.cfile
#include <stdio.h> extern int cal(int a,int b); int main(void) { printf("%d\n",cal(11,10)); return 0; }
编译测试程序,这里咱们只是链接了cal,没链接add
gcc test.c -o test -L. -lcal
最后运行test
因为咱们前面指定的环境变量,能够这么作,若是不指定环境变量,默认会从系统的库路径下查找,那么找不到so,就没法编译咱们的测试程序;
完整的Makefile
all: gcc -shared -fPIC -o libadd.so add.c gcc -shared -fPIC -o libcal.so cal.c -L. -ladd gcc test.c -o test -L. -lcal clean: rm -rf test rm -rf *.so
使用以前要提早设置环境变量;