Linux动态库与静态库

目的:验证动态库和静态库的相互依赖关系,
过程:1.一个函数的c文件(test.c)被编译成动态库(libtest.so)
2.另外一个函数的c文件(test2.c)依赖libtest.so,被编译成libtest2.a
3.主函数(main.c)调用libtest2.a中的函数
4.移走libtest.so文件(不存在的情况),main执行情况。

test.c

int f(){
    return 3;
}

编译为动态so库文件:
gcc -shared -fPIC -o libtest.so test.c 生成libtest.so

test2.c

#include <stdio.h>
extern int f();
int test2(void){
    printf("the result is %d\n",f());
    return f();
}

编译为动态a库文件:
ar rcs libtest2.a test2.c 生成libtest2.a库文件

main.c

#include <stdio.h>
extern int test2(void);
int main(){
    printf("test2 :%d\n",test2());
    return 0;
}

依赖libtest2.a文件,编译为可执行文件:gcc -o main main.c -L. -ltest ./libtest2.a

执行 ./main
输出:
the result is 3
test2 :3

把libtest.so拷贝到上一级目录,执行 gcc -o main main.c -L../ -ltest ./libtest2.a 依然可以编译成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值