1,文件目录
root@rose:~/test# tree
.
|-- hello.c
|-- hello.h
|-- hello.o
|-- libadd.a
|-- main
`-- main.c
2,创建hello.h和hello.c
2.1. hello.h
#ifndef __HELLO__
#define __HELLO__
int add(int a, int b);
#endif
2.2 hello.c
int add(int a, int b)
{
return a+b;
}
3,创建静态库
ar -rcs libadd.a hello.o # 生成静态库,libadd.a
4,创建main.c
#include<stdio.h>
#include<math.h>
#include "hello.h" # 调用静态库,引入头文件
int main()
{
double y = sin(2.0);
int sum = add(3, 5);
printf("the result = %f\n",y);
printf("the sum = %d\n", sum);
return 0;
}
5,编译
gcc -o main main.c -L ./ -ladd # -L 静态库存放路径,-ladd,具体的静态库名字
6,执行
root@rose:~/test# ./main
the result = 0.909297
the sum = 8
博客介绍了静态库创建及编译执行的步骤,包括创建文件目录,编写hello.h、hello.c和main.c文件,创建静态库,最后进行编译和执行操作。

3273

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



