使用gcc熟悉静态库和动态库
1.使用hello练习库
hello代码
hello.h
#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif//HELLO_H
hello.c
#include<stdio.h>
void hello(const char *name)
{
printf(“Hello %s\n”,name);
}
main.c
#include"hello.h"
int main()
{
hello(“everyone”);
return 0;
}
静态库的使用
创建文件和写入代码

创建.o文件

创建静态库

使用静态库运行文件

删除静态库,运行文件

动态库的使用
创建动态库

执行动态库

静态库和动态库的比较

2.静态库的使用
代码
sub1.c
float x2x(int a,int b)
{
float c=0;
c=a+b;
return c;
}
sub2.c
float x2y(int a,int b)
{
float c=0;
c=a/b;
return c;
}
sub.h
#ifndef SUB_H
#define SUB_H
float x2x(int a,int b);
float x2y(int a,int b);
#endif
main.c
#include<stdio.h>
#include"sub.h"
void main()
{
int a,b;
printf(“Please input the value of a:”);
scanf("%d",&a);
printf(“Please input the value of b:”);
scanf("%d",&b);
printf(“a+b=%.2f\n”,x2x(a,b));
printf(“a/b=%.2f\n”,x2y(a,b));
}
生成sub1.o和sub2.o文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c9Toij7I-1633612576396)(C:\Users\无\AppData\Roaming\Typora\typora-user-images\image-20211006224030076.png)]](https://i-blog.csdnimg.cn/blog_migrate/e492766bbb7b606442be2887373d73b2.png)
生成静态库

动态库的使用
生成动态库

比较生成的文件,发现静态库比动态库要小,生成的文件也有较大差异
3总结
这次对于静态库和动态库的练习,让我了解到了静态库和动态库的特点,对gcc也有了更加深入的了解。
本文详细介绍了如何使用GCC实践静态库和动态库,包括hello世界示例、代码创建与链接过程,以及两者之间的区别,帮助读者理解库的类型和gcc的运用。

被折叠的 条评论
为什么被折叠?



