Linux 使用gcc命令生成静态库和动态库

Linux 使用gcc命令生成静态库和动态库

1、生成静态库

1、创建目录保存本次文件

image-20211003181215007

2、编辑生成例子程序 hello.h、hello.c 和 main.c

image-20211003181658757

程序1:hello.h

#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif //HELLO_H

程序2:hello.c

#include <stdio.h>
void hello(const char *name)
{
printf("Hello %s!\n", name);
}

程序3:main.c

#include "hello.h"
int main()
{
hello("everyone");
return 0;
}

3、将 hello.c 编译成.o 文件

在系统提示符下键入以下命令得到 hello.o 文件。

 gcc -c hello.c

image-20211003182258082

在 ls 命令结果中,看到 hello.o 文件

4、由.o 文件创建静态库

静态库文件名的命名规范是以 lib 为前缀,紧接着跟静态库名,扩展名为.a

在系统提示符下键入以下命令将创建静态库文件 libmyhello.a。

  ar -crv libmyhello.a hello.o

image-20211003182548691

在ls 命令结果中有 libmyhello.a

5、在程序中使用静态库

在系统提示符下键入以下命令生成目标程序hello

gcc main.c libmyhello.a -o hello

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值