diff --git a/day8/d2/README.md b/day8/d2/README.md new file mode 100644 index 0000000..e40b1c1 --- /dev/null +++ b/day8/d2/README.md @@ -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 . +``` diff --git a/day8/d2/my.c b/day8/d2/my.c new file mode 100644 index 0000000..044dcb9 --- /dev/null +++ b/day8/d2/my.c @@ -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; +} \ No newline at end of file diff --git a/day8/d2/my/includes/my.h b/day8/d2/my/includes/my.h new file mode 100644 index 0000000..26ce97f --- /dev/null +++ b/day8/d2/my/includes/my.h @@ -0,0 +1,9 @@ +#ifndef __MY_H__ +#define __MY_H__ + +#include + +int sum(int, int); +int sub(int, int); + +#endif \ No newline at end of file diff --git a/day8/d2/my/libs/libmy.so b/day8/d2/my/libs/libmy.so new file mode 100755 index 0000000..b03c480 Binary files /dev/null and b/day8/d2/my/libs/libmy.so differ diff --git a/day8/d2/test.c b/day8/d2/test.c new file mode 100644 index 0000000..fe77231 --- /dev/null +++ b/day8/d2/test.c @@ -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; +} \ No newline at end of file