From 67ac07b1a4b0aae9021d6e7ce1273710694fdd6f Mon Sep 17 00:00:00 2001 From: flykhan Date: Wed, 12 Jul 2023 11:41:46 +0800 Subject: [PATCH] =?UTF-8?q?day8=20coding:=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day8/d2/README.md | 18 ++++++++++++++++++ day8/d2/my.c | 11 +++++++++++ day8/d2/my/includes/my.h | 9 +++++++++ day8/d2/my/libs/libmy.so | Bin 0 -> 7896 bytes day8/d2/test.c | 9 +++++++++ 5 files changed, 47 insertions(+) create mode 100644 day8/d2/README.md create mode 100644 day8/d2/my.c create mode 100644 day8/d2/my/includes/my.h create mode 100755 day8/d2/my/libs/libmy.so create mode 100644 day8/d2/test.c 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 0000000000000000000000000000000000000000..b03c4803ed6356d472a304901d19e53331720cd1 GIT binary patch literal 7896 zcmeHMU2I%O6`u9Ru@gIM$B^JAm9hf2q=a5MCN?f5WF33$n_wrwv85s~H+Q{j@1p&2 zckc!}qF~9P)QwUiUO;_8Na2B&N&)deg@jTiDwRMe6#_-&B@$FPs0de3$@Qz;XaI#hC~%)4oQ5dizj=3R{iJTM(Y)N!{RhSLaJ%oP zCrr=SscEtB!_9Nk@BOOvnz;G7(1EA__E!Arqud1Rc}W<@{d?|uv{{VAcCc&*J?-=y zkN@VKpZ@-vUw-lB=h|*R{nW#+UVb<7r(gdi@(toi-LQm3t2k(EhKI>I8i3&TgXC@l z`CtS2B*{NWk6FG>f=;nVL_4E3su!hvuQ+JP%Ovj-ZDK($8qXz{7wG8}d+Yr0dUXYi zcdwMUi*Fl?za-^t;-Z#!5ig75%;ySur{uXs&vAs4$!9#_%n=dJiKz)EnJT8{GbJxo zoSGQU7V@bnH<3*#U9iTPU3A%jo6TGx!;|NoN$p`c>y}EXk|>pP^dv+!lbE$i1#3V! zsif<zTJBg6SDK)K_S^BzZ8^U3jTvTofc_ixz^pWMHM_R2f0 z_NC@mc1DH$wd-E<+K0%Ay>hel23k_I(*GkesXX#ydUqU%QSAcN=B`v8d0tZc^3m^8 z#lGCkWZHgg!(M&cUfGD8-$U+4Sw-%5%vA2obsQKGV&(>WnX@_$T$BwEJ|pq{35DNe z{A-(Gdv)D@ZS9yHdfk3=)9WT@yS1}+W z5VsbuR4QvWy=Go-UME55ul4=8*Fv3MIV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MXGP$L4QsfKfW60>IU$gzyWZew+uYu z;cktGorwqkTdJ@P#*cU<+5oSo190&>sHhoAkwYZMd|+Pr`wD7*c@KLL;+oN*!HRzZF=mpl literal 0 HcmV?d00001 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