1、静态库命名规则
lib+ 库名字+.a
如libmytest.a
2、静态库制做步骤
1)生成对应文件.o文件
.c --> .o 用.c
2) 将生成的.o文件打包
ar rcs + 生成静态库的名字(libmytest.a) +生成的全部的.o
发布静态库
gcc test.c -I ./include -L mytest -o app
gcc test.c lib/libmytest.a -o test -I ./include
3、演示
目录结构

head.h
#ifndef _HEAD_H_
#define _HEAD_H_
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
int div(int a, int b);
#endif
add.c
#include "head.h"
int add(int a, int b)
{
return a + b;
}
sub.c
#include "head.h"
int sub(int a, int b)
{
return a - b;
}
mul.c
#include "head.h"
int mul(int a, int b)
{
return a * b;
}
div.c
#include "head.h"
int div(int a, int b)
{
return a / b;
}
编译步骤
cd到src目录下
1、gcc *.c -c -I ../include
2、ar rcs libMyCalc.a *.o
3、mv libMyCalc.a ../lib
测试代码 main.c
#include <stdio.h>
#include "head.h"
int main()
{
int sum = add(5, 6);
printf("The sum 5 + 6 = %d\n", sum);
return 0;
}
编译测试程序
gcc main.c -I include -L lib -l MyCalc -o app
./app
使用nm指令查看.a 和app

5163

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



