gcc动态库和静态库的使用

-Wl选项告诉编译器将后面的参数传递给链接器。
-rpath使得execute记住链接库的位置.
-W -rpath=./调用当前文件中的.so文件

动态库

#include <stdio.h>
#include"test2.h"
int main(void)
{
    func_a();
    struct St *s= func_b();
    printf("hello\n");
    // printf("%s\n",s.name);
    return 0;
}
#include <stdio.h>
#include "test2.h"
#include<malloc.h>
struct St{
    int id;
    char *name;
};
void func_a(){
    printf("FUNC_A\n");
}
struct St *func_b(){
    struct St *s = malloc(sizeof(struct St));
    s->id=123;
    s->name="Luck";
    return s;
}
#ifndef TEST2_H
#define  TEST2_H
struct St;
void func_a();
struct St * func_b();
#endif

将test2.c编译成test2.so库
gcc -shared test2.c -o test2.so

将test1进行编译
gcc test1.c test2.so -L. -o test1 -Wl,-rpath=./
运行 ./test1
在这里插入图片描述
当test1和test2.so不处于同一个文件夹下时报错
在这里插入图片描述

静态库

gcc -c test2.c
ar rcs test2.a test2.o
gcc test1.c -L ./ test2.a -o test1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值