2020年3月20日 数据结构课程设计作业

简单写了一个两个数输入计算器小项目

功能:加 、减、乘、除、求余、

一、add.c

int add(int num1,int num2)
{ 
return num1+num2;

}

二、sub.c

 
 
 
 
int sub(int num1,int num2){
return num1-num2;

}
 
  
 
 

三、mul.c

int mul(int num1,int num2){
return num1*num2;

}

四、div.c

int div(int num1,int num2){
if(num2==0){
return -1;
}
else{
return num1/num2;
}

}

五、mod.c

int mod(int num1,int num2){
if(num1>=num2){
return num1%num2;}
else{
return num1;
}

}

六、lib.h

#ifndef cal_h
#define cal_h

int add(int,int);
int sub(int,int);
int mul(int,int);
int div(int,int);
int mod(int,int);

 

#endif

七、test.c

#include<stdio.h>
#include"cal.h"

int main(){
int num1,num2;
printf("请输入一个数:\n");
scanf("%d",&num1);
printf("请再输入一个数:\n");
scanf("%d",&num2);
printf("%d add %d = %d\n",num1,num2,add(num1,num2));
printf("%d sub %d = %d\n",num1,num2,sub(num1,num2));
printf("%d mul %d = %d\n",num1,num2,mul(num1,num2));
printf("%d div %d = %d\n",num1,num2,div(num1,num2));
printf("%d mod %d = %d\n",num1,num2,mod(num1,num2));
return 0;

}

1、没用库实现,用Makefile编译一下(对Makefile编译不熟,就拿项目这个开刀吧)

首先是Makefile文件的编写(觉得老师讲的很清楚)

OBJ=test.o add.o sub.o mul.o div.o mod.o 
test:$(OBJ) cal.h
gcc $(OBJ) -o test

.PHONY:clean cleanA
cleanA:
rm test $(OBJ)
clean:
rm $(OBJ)

编写完成之后,使用make命令后,再用命令ls -l,你会发现Makefile是真的好用,显示结果如下:

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork# ls -l
total 45
-rw-r--r-- 1 root root  152 Mar 21 18:13 Makefile
-rw-r--r-- 1 root root   57 Mar 21 17:58 add.c
-rw-r--r-- 1 root root 1232 Mar 21 18:16 add.o
-rw-r--r-- 1 root root  152 Mar 21 20:27 cal.h
-rw-r--r-- 1 root root  121 Mar 21 17:59 div.c
-rw-r--r-- 1 root root 1248 Mar 21 18:16 div.o
-rw-r--r-- 1 root root  115 Mar 21 18:00 mod.c
-rw-r--r-- 1 root root 1248 Mar 21 18:16 mod.o
-rw-r--r-- 1 root root   51 Mar 21 17:59 mul.c
-rw-r--r-- 1 root root 1232 Mar 21 18:16 mul.o-rw-r--r-- 1 root root   52 Mar 21 17:58 sub.c
-rw-r--r-- 1 root root 1232 Mar 21 18:16 sub.o
-rwxr-xr-x 1 root root 8792 Mar 21 20:29 test
-rw-r--r-- 1 root root  545 Mar 21 20:29 test.c
-rw-r--r-- 1 root root 2744 Mar 21 20:29 test.o

从上面可以看出,没使用库大概是8k多

2.使用静态库后的效果

使用静态库文件,生成静态库。库文件以lib为前缀,紧接着是库的名称,扩展名为.a,例如我们这里要创建库名libcal.a的库,使用命令ar,具体如下:(忘记了用自己的名字命名)

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/croot@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork# 
ar rcs libcal.a add.o sub.o mul.o div.o mod.o 

使用静态库,创建库文件的接口文件头文件,本文中是cal.h文件,使用库文件的文件test.c包含头文件cal.h文件即可,使用如下命令编译:

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSample# 
gcc -o test test.c -static -L. -lcal

其中上边命令的说明:

  (1)、gcc -o test:使用gcc编译,-o指定文件名,后边的test就是最终生成的文件名

  (2)、-static:指明使用静态库

  (3)、-L.:-L指明使用库,后面的.表明库文件在当前目录

  (4)、-lcal:表明是库文件的名称,其中-表明是选项,l是lib的简写,后边的zh才是真正的库文件名称,后缀名是不需要的

  查看文件的大小:

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSample# ls -l
total 952
-rw-r--r-- 1 root root    133 Mar 21 21:01 cal.h
-rw-r--r-- 1 root root   6604 Mar 21 21:01 libcal.a
-rwxr-xr-x 1 root root 958600 Mar 21 21:04 test
-rw-r--r-- 1 root root    489 Mar 21 21:01 test.c

可见:通过上面的显示可以看出,使用静态库文件的可执行文件的大小为958600字节,也就是958k这无疑是非常大的,所以这也是静态库的一个弊端也就是太大了。(太可怕了)

 3、使用动态库文件:生成动态库文件。库文件一般以lib为前缀,紧接着是库的名称,扩展名为.so,我们这里要创建库名libcal.so的库,具体如下:

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# gcc -shared -fPIC -o libcal.so *.o
root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# ls
add.o  div.o      mod.o  sub.o
cal.h  libcal.so  mul.o  test.c

可见我的目录就有了libcal.so这个库

再使用以下命令就可实现libcal.so这个库

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# gcc -o test test.c -L. -lcal

接下来是测试建立的库文件是否能正确的运行起来:(意外的是竟然出现一个问题)

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# d object file: No such file or directory

随后就去老师博客上看到解决问题的方法,注意这里只用了一种:

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# export  LD_LIBRARY_PATH=$(pwd)
root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# ls
add.o  cal.h  div.o  libcal.so  mod.o  mul.o  sub.o  test  test.c

之后就有用啦!

root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# ./test
请输入一个数:
1
请再输入一个数:
3
1 add 3 = 4
1 sub 3 = -2
1 mul 3 = 3
1 div 3 = 0
1 mod 3 = 1
root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# ls -l
total 44
-rw-r--r-- 1 root root 1232 Mar 21 21:24 add.o
-rw-r--r-- 1 root root  133 Mar 21 21:24 cal.h
-rw-r--r-- 1 root root 1248 Mar 21 21:24 div.o
-rwxr-xr-x 1 root root 7664 Mar 21 21:42 libcal.so
-rw-r--r-- 1 root root 1248 Mar 21 21:24 mod.o
-rw-r--r-- 1 root root 1232 Mar 21 21:24 mul.o
-rw-r--r-- 1 root root 1232 Mar 21 21:24 sub.o
-rwxr-xr-x 1 root root 8584 Mar 21 21:42 test
-rw-r--r-- 1 root root  489 Mar 21 21:24 test.c

可见动态库只比没用库大一点而已,但是比静态库小好多好多,但是动态库也出现了一点问题,还是相当好用的

结合老师的博客,以及教学视频,操作流程如上文,可能博客不是特别好,但是这是对我学习的一个检验,希望以后有更多的机会写博客,努力学习,最后我发现我爱上了编程,我要学好编程!耶耶耶!。

 附上参考原文地址:https://www.cnblogs.com/guochaoxxl/p/7141447.html

猜你喜欢

转载自www.cnblogs.com/wang-15907069255/p/12543071.html