day8 coding: 动态库的生成和使用
This commit is contained in:
parent
0baf7f02f8
commit
67ac07b1a4
|
@ -0,0 +1,18 @@
|
|||
### 动态库 (libxx.so) 生成
|
||||
|
||||
```bash
|
||||
# 生成 xx 工具的动态库 xx.c ---> libxx.so
|
||||
gcc -shared xx.c -o libxx.so
|
||||
```
|
||||
|
||||
### 静态库的使用
|
||||
|
||||
```bash
|
||||
# test.c 使用 libxx.so 编译生成可执行文件 test 的方式
|
||||
gcc test.c libxx.so -o test
|
||||
# 运行时需要将库文件的目录 (./是当前路径) 添加到 LD_LIBRARY_PATH 环境变量中
|
||||
export LD_LIBRARY_PATH=./my/libs:$LD_LIBRARY_PATH
|
||||
# 或者将生成的库文件和相关头文件添加 (或使用软连接) 到系统的库目录 (/usr/lib) 及系统的头文件目录 (/usr/include)中
|
||||
# 以 root 进入 /usr/lib 目录,然后创建软连接
|
||||
ln -s /动态库目录.../libxx.so .
|
||||
```
|
|
@ -0,0 +1,11 @@
|
|||
#include "./my/includes/my.h"
|
||||
|
||||
int sum(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int sub(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef __MY_H__
|
||||
#define __MY_H__
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int sum(int, int);
|
||||
int sub(int, int);
|
||||
|
||||
#endif
|
Binary file not shown.
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue