From e8fdb609fe03969a81c60510cb0ea4657d33ef16 Mon Sep 17 00:00:00 2001 From: flykhan Date: Wed, 12 Jul 2023 23:16:19 +0800 Subject: [PATCH] =?UTF-8?q?day8=20homework:=20=E5=8A=A8=E6=80=81=E5=BA=93?= =?UTF-8?q?=E7=9A=84=E7=94=9F=E6=88=90=E5=92=8C=E4=BD=BF=E7=94=A8=E9=A2=98?= =?UTF-8?q?=E7=9B=AE=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day8/homework/h1/README.md | 32 +++++++++++++++++++++++++++++++ day8/homework/h1/includes/a.h | 6 ++++++ day8/homework/h1/includes/b.h | 6 ++++++ day8/homework/h1/libs/liba.so | Bin 0 -> 7976 bytes day8/homework/h1/libs/libb.so | Bin 0 -> 7872 bytes day8/homework/h1/tempfiles/a.c | 10 ++++++++++ day8/homework/h1/tempfiles/a.h | 6 ++++++ day8/homework/h1/tempfiles/b.c | 20 +++++++++++++++++++ day8/homework/h1/tempfiles/b.h | 6 ++++++ day8/homework/h1/testArrSort.c | 24 +++++++++++++++++++++++ day8/homework/h1/testFactorial.c | 11 +++++++++++ 11 files changed, 121 insertions(+) create mode 100644 day8/homework/h1/README.md create mode 100644 day8/homework/h1/includes/a.h create mode 100644 day8/homework/h1/includes/b.h create mode 100755 day8/homework/h1/libs/liba.so create mode 100755 day8/homework/h1/libs/libb.so create mode 100644 day8/homework/h1/tempfiles/a.c create mode 100644 day8/homework/h1/tempfiles/a.h create mode 100644 day8/homework/h1/tempfiles/b.c create mode 100644 day8/homework/h1/tempfiles/b.h create mode 100644 day8/homework/h1/testArrSort.c create mode 100644 day8/homework/h1/testFactorial.c 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 0000000000000000000000000000000000000000..7eb3f29096604eb358d7627a46e9058a06904ecf GIT binary patch literal 7976 zcmeHMTWlQF89wW^W9PQJPD^5vNEx9-7y{GH#jewk#+R%!iJV{@TcVZH$#^gJMtezj zXHz>W5Y?2{O)aAK1y#J{fd>Q#fsg>fgHaHKH!ebiK1He`IU;I9D^My$-v0lY^Q~vk zj#0z|QY9a2=0E@UU(R*T{^$2oiJ`%!P)MMJ#m@zE=UY6aR|z^hRE0>d=n<{--YV{u zx=M9P!B=~v)nh<~FeJcjtm0ZkvP2E9qG)H6$HoMl=)U z=Zcc+bi2|6WnC`A*WDD?-;$keq+6wypt>eXKWuheCO=3zsqS{+-UIgYtQ8gSn zwjYy^Fg=HlkBPabtuOnFr=R=$p*?^4$(7i%_kG&_CO5%yUJ}gzpXZGh(cHV5vDNgf zqo+Ud`OSC!@t5EJE!Oew)n7lp_3gQ@BVT>?FRT=41eY**=+rg?&x;YD&g}=`&N6t< zGWZDL_tB%5Sps5Wqv(tob)x4bzDdM2yh8W}8vS{-XgihFMWwI%;q}@OFy4(4UoT$P z7vHug^2LH(cAb)I+rrKkvaYZ*M1*~0^q8GWm(mm2vYRfA9vjFN3+Yj3JeT%l!4f+; z<*)-Mmpw;{N6*?L%ELg;DVNh_k#Ul4v6OXkB9|Rcn&qO|E$np4aUEgXJPpL__t-Y0+ z%fcR#STpapU5187t6i^?N_EE@^p0-pr5f{-EWB~8N@d4s;xiYkJI+aJ%^kQz6>F}A z$(Z%PZENXX%Lndr7dLY%zJUNdHN+tac^vUj9?*DpQ$MYex$ zF>KA=vff)bXoWtoK3Q}-$XQ4^Tjx3R>~(+V544k-sN8SO9Qa~2)yBx5yUx0B;NQe8 zBxnQ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); +} \ 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