I wrote a simple two input calculator number of small projects
Functions: addition, subtraction, multiplication, division, remainder,
A, add.c
int add(int num1,int num2) { return num1+num2; }
Two, sub.c
int sub(int num1,int num2){ return num1-num2; }
Three, mul.c
int mul(int num1,int num2){ return num1*num2; }
Four, div.c
int div(int num1,int num2){ if(num2==0){ return -1; } else{ return num1/num2; } }
Five, mod.c
int mod(int num1,int num2){ if(num1>=num2){ return num1%num2;} else{ return num1; } }
Six, lib.h
cal_h #ifndef #define cal_h you add ( you , you ); you intended ( you , you ); you executed ( you , you ); you div ( you , you ); you way ( you , you ); #endif
Seven, test.c
#include <stdio.h> #include " cal.h " int main () { int num1, num2; the printf ( " Enter a number: \ n- " ); Scanf ( " % D " , & num1); the printf ( " Please enter a number: \ n- " ); Scanf ( " % D " , & num2); the printf ( " % D the Add% D =% D \ n- ' , num1, num2, the Add (num1, num2)); the printf ( " % D% =% D Sub D \ n- " , num1, num2, Sub (num1, num2)); the 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, useless library implementation, compile it with a Makefile (Makefile for compiling unfamiliar, so they took this project cut it)
The first is the preparation of the Makefile (think the teacher said very clearly)
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)
After completion, after using the make command, then the command ls -l, you will find Makefile is really easy to use, show the following results:
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 r is-rw - r 1 to set a root to set a root , Canada 121 Mar Do not 21 And 17 And : of 59 divc rw-r is - r 1 to set a root to set a root 1248 Mar Do not 21 And the 18- : 16 And divo rw-r is - r- - 1 to set a root to set a root a 115- Mar Do not 21 And the 18- : '00' modc rw-r is - r 1 to set a root to set a root 1248 Mar Do not 21 And the 18- : 16 And modo rw-r is - r 1 to set a root to set a root 51 And Mar Do not 21 And 17 And : 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
As can be seen from the above, do not use the library is about more than 8k
2. Use the effect after the static library
Use a static library files, generate static libraries. Library files are prefixed with lib, followed by the library name, extension .a, for example, we are here to create a library name libcal.a library, use the command ar, as follows :( forgot about his name)
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
Use static library, create a library file interface header file, this article is cal.h files, use the library file test.c file to include the header file cal.h, use the following command to compile:
root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSample# gcc -o test test.c -static -L. -lcal
Where the top command description:
(1), gcc -o test: Use gcc compiler, -o specifies the file name, behind the test is the final generated file name
(2), - static: specifies the use of static libraries
(3), - L.:-L specified using the library, the latter indicates that the library file in the current directory.
(4), - lcal: indicate the name of the library file, where - indicates that option, l is shorthand lib, zh is the real name behind the library file, the suffix is not needed
View file size:
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
Visible: the display can be seen from the above, using the static library file size of the executable file is 958,600 bytes, which is 958k This is undoubtedly a very large, so this is a drawback static library is too big. (too terrifying)
3, using a dynamic library files: a dynamic library files. Library files are generally prefixed with lib, followed by the library name, extension .so, we are here to create a library name libcal.so library, as follows:
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
I have seen a catalog of the library libcal.so
Then use the following command can achieve libcal.so library
root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# gcc -o test test.c -L. -lcal
The next test is whether the establishment of the library up and running correctly :( surprised that actually had a problem)
root@LAPTOP-84Q4S2MM:/home/wangshuibin/code/ccode/sample/classwork/libSo# d object file: No such file or directory
Then I went to see the teacher blog solution to the problem, only a note here:
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
After you have used it!
@ LAPTOP-84Q4S2MM the root: / Home / wangshuibin / code / ccode in / Sample / classwork / libSo ./ # Test Enter a Number: 1 Please enter a number from: . 3 1 the Add . 3 = . 4 1 Sub . 3 = - 2 1 MUL . 3 = . 3 . 1 div . 3 = 0 . 1 MOD . 3 = . 1 the root @ LAPTOP -84Q4S2MM: / Home / wangshuibin / code / ccode in / Sample / classwork / libSo LS # - L Total 44 is -rw-R & lt - r-- . 1 the 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
Visible only DLL library just a little larger than useless, but many, many smaller than the static library, dynamic library but also appeared a little problem, is still quite easy to use
Combined with the teacher's blog, as well as instructional videos, operational processes as described above, may blog is not particularly good, but this is a test for me to learn, and hope to have more opportunities to blog, trying to learn, finally, I found that I fell in love with programming, I want to learn programming! Yeye Ye! .
Attach refer to the original address: https: //www.cnblogs.com/guochaoxxl/p/7141447.html