day8 coding: 静态库的生成和使用

This commit is contained in:
flykhan 2023-07-12 10:38:10 +08:00
parent ff10f99477
commit 06c95ae1a1
4 changed files with 35 additions and 0 deletions

17
day8/d1/README.md Normal file
View File

@ -0,0 +1,17 @@
### 静态库生成
```bash
# 汇编源文件 xxx.c ---> xxx.o
gcc -c xxx.c -o xxx.o
# 使用汇编后的源文件生成静态库 xxx.o ---> libxxx.a
ar rc libxxx.a xxx.o
```
### 静态库的使用
```bash
# 生成可执行文件 a.out
gcc -static test.c -L/home/.../libs -lxxx -I/home/.../includes
# 生成自定义可执行文件 test
gcc -static test.c -o test -L/home/.../libs -lxxx -I/home/.../includes
```

9
day8/d1/my/includes/my.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __MY_H__
#define __MY_H__
#include <stdio.h>
extern int sum(int, int);
extern int sub(int, int);
#endif

BIN
day8/d1/my/libs/libmy.a Normal file

Binary file not shown.

9
day8/d1/test.c Normal file
View File

@ -0,0 +1,9 @@
#include "./my/includes/my.h"
int main()
{
int a = 10, b = 20;
printf("10+20=%d\n", sum(a, b));
printf("20-10=%d\n", sub(b, a));
return 0;
}