diff --git a/day8/homework/h1/README.md b/day8/homework/h1/README.md new file mode 100644 index 0000000..c095e0b --- /dev/null +++ b/day8/homework/h1/README.md @@ -0,0 +1,32 @@ +**#### 题目要求: 创建 a.h、a.c,、b.h、b.c 四个文件,在 a 文件中创建求阶乘函数并实现,在 b 文件中创建数组的排序函数并实现,要求将 a,b 编译成动态库文件。并编写 test.c 文件,引入 a 和 b 两个文件的功能并编译运行。** + +> 1. `libs` 中包含使用 `tempfiles` 文件夹中的源代码生成的动态库 +> 2. `includes` 中包含 `test*.c` 需要用到的头文件 + +### 生成动态库 + +```bash +# 一般生成方式 +gcc -shared a.c -o liba.so + +# 如果碰到 /usr/bin/ld: /tmp/ccojI2tR.o: relocation R_X86_64_PC32 against symbol `factorial' can not be used when making a shared object; recompile with -fPIC , 可以使用如下命令 +gcc -c -fPIC source_file.c -o object_file.o +gcc -shared object_file.o -o libshared_object.so +``` + +> 关于 -fPIC 的问题: +> `-fPIC` 是 GCC(GNU Compiler Collection)编译器的一个选项,用于生成位置无关代码(Position Independent Code,PIC)。 +> +> 位置无关代码是一种机器代码,它可以在内存中的任意位置加载和执行,而不受具体加载地址的限制。这对于共享对象(动态链接库)非常重要,因为共享对象可以在内存中的不同地址加载,并被多个进程或程序共享使用。 +> +> 使用 `-fPIC` 选项编译源代码时,编译器会生成适用于位置无关的目标文件。这些目标文件可以在后续的链接过程中用于创建共享对象。 +> +> 通过使用 `-fPIC`,编译器会使用一些技术来生成位置无关代码,例如使用相对寻址(relative addressing)而不是绝对寻址(absolute addressing),以及使用全局偏移表(Global Offset Table,GOT)等。 +> +> 总之,`-fPIC` 选项告诉编译器生成位置无关代码,以便在创建共享对象时能够正确地进行链接和加载,避免出现链接错误。 + +### 编译时指定头文件可以使用 + +```bash +gcc testxxx.c ./libs/libxxx.so -I ./includes/ -o testxxx +``` diff --git a/day8/homework/h1/includes/a.h b/day8/homework/h1/includes/a.h new file mode 100644 index 0000000..b34462a --- /dev/null +++ b/day8/homework/h1/includes/a.h @@ -0,0 +1,6 @@ +#ifndef __A_H__ +#define __A_H__ + +int factorial(int); // 声明阶乘函数 + +#endif \ No newline at end of file diff --git a/day8/homework/h1/includes/b.h b/day8/homework/h1/includes/b.h new file mode 100644 index 0000000..9009917 --- /dev/null +++ b/day8/homework/h1/includes/b.h @@ -0,0 +1,6 @@ +#ifndef __B_H__ +#define __B_H__ + +int *arrSort(int arrInput[], int arrlen); // 声明数组排序函数 + +#endif \ No newline at end of file diff --git a/day8/homework/h1/libs/liba.so b/day8/homework/h1/libs/liba.so new file mode 100755 index 0000000..7eb3f29 Binary files /dev/null and b/day8/homework/h1/libs/liba.so differ diff --git a/day8/homework/h1/libs/libb.so b/day8/homework/h1/libs/libb.so new file mode 100755 index 0000000..bd66eec Binary files /dev/null and b/day8/homework/h1/libs/libb.so differ diff --git a/day8/homework/h1/tempfiles/a.c b/day8/homework/h1/tempfiles/a.c new file mode 100644 index 0000000..1aa104c --- /dev/null +++ b/day8/homework/h1/tempfiles/a.c @@ -0,0 +1,10 @@ +#include "a.h" +#include + +int factorial(int n) +{ + if (n == 0) + return 1; + else + return n * factorial(n - 1); +} \ No newline at end of file diff --git a/day8/homework/h1/tempfiles/a.h b/day8/homework/h1/tempfiles/a.h new file mode 100644 index 0000000..b34462a --- /dev/null +++ b/day8/homework/h1/tempfiles/a.h @@ -0,0 +1,6 @@ +#ifndef __A_H__ +#define __A_H__ + +int factorial(int); // 声明阶乘函数 + +#endif \ No newline at end of file diff --git a/day8/homework/h1/tempfiles/b.c b/day8/homework/h1/tempfiles/b.c new file mode 100644 index 0000000..ba4fe1d --- /dev/null +++ b/day8/homework/h1/tempfiles/b.c @@ -0,0 +1,20 @@ +#include "b.h" +#include + +int *arrSort(int arrInput[], int arrlen) +{ + int i, j, temp; + for (i = 0; i < arrlen - 1; i++) + { + for (j = 0; j < arrlen - 1 - i; j++) + { + if (arrInput[j] > arrInput[j + 1]) + { + temp = arrInput[j]; + arrInput[j] = arrInput[j + 1]; + arrInput[j + 1] = temp; + } + } + } + return arrInput; +} \ No newline at end of file diff --git a/day8/homework/h1/tempfiles/b.h b/day8/homework/h1/tempfiles/b.h new file mode 100644 index 0000000..9009917 --- /dev/null +++ b/day8/homework/h1/tempfiles/b.h @@ -0,0 +1,6 @@ +#ifndef __B_H__ +#define __B_H__ + +int *arrSort(int arrInput[], int arrlen); // 声明数组排序函数 + +#endif \ No newline at end of file diff --git a/day8/homework/h1/testArrSort.c b/day8/homework/h1/testArrSort.c new file mode 100644 index 0000000..384a063 --- /dev/null +++ b/day8/homework/h1/testArrSort.c @@ -0,0 +1,24 @@ +#include "b.h" +#include + +int main() +{ + int N; // 定义数组长度 + printf("Please input the length of the array: "); + scanf("%d", &N); + int arrInput[N]; // 未初始化的数组 + printf("Please input %d numbers: ", N); + int i = 0; + while (i < N) + { + scanf("%d", &arrInput[i++]); + } + + int *arrOutput = arrSort(arrInput, N); + for (int i = 0; i < N; i++) + { + printf("%d ", arrOutput[i]); + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/day8/homework/h1/testFactorial.c b/day8/homework/h1/testFactorial.c new file mode 100644 index 0000000..d39cf5e --- /dev/null +++ b/day8/homework/h1/testFactorial.c @@ -0,0 +1,11 @@ +#include "./includes/a.h" +#include + +int main() +{ + int n; + printf("Please input a number: "); + scanf("%d", &n); + printf("%d! = %d\n", n, factorial(n)); + return 0; +} \ No newline at end of file