Use linux library

This procedure is primarily the function of three variables and the calculated maximum, minimum and average!

max.c

int max(int var1,int var2,int var3){
    int x;
     if(var1>var2){
     if(var1>var3) x=var1;
      }
      else {
      if(var2>var3) x=var2;
      else x=var3;
      }
      return x;
}

add.c

int add(int var1, int var2,int var3){
    return var1 + var2 + var3;
}

min.c

int min(int var1,int var2,int var3)
{            int y;
     if(var1<var2)
{     if(var1<var3) y=var1;
     }
     else {
     if(var2<var3) y=var2;
     else y=var3;
     }
     return y;
}

avg.c

int avg(int var1,int var2,int var3){
    return (var1+var2+var3)/3;

}

testcb.c

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

int main(int argc, char **argv)
{
    int var1;
    int var2;
    int var3;
    printf("please input the var1: ");
    scanf("%d", &var1);
    printf("please input the var2: ");
    scanf("%d", &var2);
    printf("please input the var3: ");
    scanf("%d", &var3);
    printf("the add is %d\n", add(var1, var2,var3));
    printf("the max is %d\n", max(var1, var2,var3));
    printf("the min is %d\n", min(var1, var2,var3));
    printf("the avg is %d\n", avg(var1, var2,var3));
    return 0;
}

cb.h

cb_h #ifndef
 #define cb_h you add ( you , you , you );
you max ( you , you , you );
you who ( you , you , you );
you avg ( you , you , you ); #endif



The following are additional file Makefile

OBJ=testcb.o add.o max.o min.o avg.o
testcb:$(OBJ) cb.h
        gcc -o testcb $(OBJ) 
testcb.o:testcb.c
add.o:add.c
max.o:max.c
min.o:min.c
avg.o:avg.c

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

By gcc * .c -o tescb compile,. / Testcb run. The results are as follows

dingxin@dingxin-VirtualBox:~/Study1$ ./a.out
please input the var1: 1
please input the var2: 2
please input the var3: 3
the add is 6
the max is 3
the min is 1
the avg is 2

By make

dingxin@dingxin-VirtualBox:~/Study1$ make
cc    -c -o testcb.o testcb.c
cc    -c -o add.o add.c
cc    -c -o max.o max.c
cc    -c -o min.o min.c
cc    -c -o avg.o avg.c
gcc -o testcb testcb.o add.o max.o min.o avg.o 

View File Size

Atto-Dingxin Dingxin VirtualBox: ~ / Study1 $ Ls - L 
总用amount 72 
-Rw-R - R-- 1 Dingxin Dingxin   119 March   22  18 : 06 Add.C
 -Rw-R - R-- 1 Dingxin Dingxin 1240 March   22  18 : 06 Add.O
 -rwxr-xr-x 1 Dingxin Dingxin 8640 March   22  18 : 21 . a out 
-rw-r - r-- 1 Dingxin Dingxin    71 March   22  18 : 06 avg. c
-R & lt -rw - r-- . 1 Dingxin Dingxin 1264 . 3 dated   22 is  18 is : 06 avg.o
 -rw-R & lt - r-- . 1 Dingxin Dingxin   209 . 3 dated   22 is  18 is : 05 cb.h
 -rw-r-- r-- . 1 Dingxin Dingxin   265 . 3 dated   22 is  18 is : 23 is the Makefile
 -rw-R & lt - r-- . 1 Dingxin Dingxin   189 . 3 dated   22 is  . 17 : 39 max.c
 -rw-R & lt - r-- . 1 Dingxin Dingxin 1280 . 3 January   22 18 : 05 Max.O
 -Rw-R - R-- 1 Dingxin Dingxin   189 March   22  18 : 05 Min.C
 -Rw-R - R-- 1 Dingxin Dingxin 1280 March   22  18 : 05 Min.O
 Xr---Rwxr X 1 Dingxin Dingxin 8640 March   22  18 : 22 Testcb
 -Rw-R - R-- 1 Dingxin Dingxin   572 March   22  18 : 08 Testcb.C
 -Rw-R - R-- 1dingxin dingxin 2720 3 Yue   22  18 : 08 testcb.o

Use make cleanA clear .o file

dingxin@dingxin-VirtualBox:~/Study1$ make cleanA
rm testcb testcb.o add.o max.o min.o avg.o   

Second, using static library files, generate static libraries. Prefixed with lib library files, create library library name libcb.a me here, use the command ar, as follows

RCS air libcb.a add.o max.o min.o avg.o

Use static library, create a library file interface header file, this article is cb.h files, use the library file testmi.c include the header file cb.h file to, use the following command to compile:

gcc -o testcb testcb.c -static -L. -l cb

View File Size

Atto-Dingxin Dingxin VirtualBox: ~ / Study1 / Libsamp $ Ls - L 
总用amount of 956 
-Rw-R - R-- 1 Dingxin Dingxin     209 March   22  18 : 49 Cb.H
 -Rw-R - R-- 1 Dingxin Dingxin    5408 March   22  18 : 49 Libcb.A
 -rwxr-xr-x 1 Dingxin Dingxin nine hundred fifty-eight thousand four hundred eighty-eight March   22  18 : 54 TESTCB
 -rw-r - r-- 1 Dingxin Dingxin     572 March   22  18 : 49 TESTCB. c

Third, the use of dynamic libraries: a dynamic library files. Library files are generally prefixed with lib, followed by the library name, extension .so, create library library name libcb.so me here, as follows

ar rcs libcb.so add.o max.o avg.o min.o

Then compile

gcc -shared -fPIC libcb.so add.o max.o min.o avg.o

carry on

gcc -o testcb testcb.c -L. -lcb

View File Size

Atto-Dingxin Dingxin VirtualBox: ~ / Study1 $ Ls - L 
总用amount 88 
-Rw-R - R-- 1 Dingxin Dingxin   119 March   22  18 : 06 Add.C
 -Rw-R - R-- 1 Dingxin Dingxin 1240 March   22  18 : 33 Add.O
 -Rwxr-Xr-X 1 Dingxin Dingxin 7608 March   22  19 : 06 . A Out 
-Rw-R - R-- 1 Dingxin Dingxin    71 March   22  18 : 06 Avg. c
-R & lt -rw - r-- . 1 Dingxin Dingxin 1264 . 3 dated   22 is  18 is : 33 is avg.o
 -rw-R & lt - r-- . 1 Dingxin Dingxin   209 . 3 dated   22 is  18 is : 05 cb.h
 -rw-r-- r-- . 1 Dingxin Dingxin 5408 March   22 is  18 is : 40 libcb.a
 -rw-R & lt - r-- . 1 Dingxin Dingxin 5408 March   22 is  . 19 : 06 libcb.so 
drwxr -XR X- 2 Dingxin Dingxin 4096 March  22 is  . 19 : 05 libsamp
 -rw-R & lt - r-- . 1 Dingxin Dingxin   265 . 3 dated   22 is  18 is : 23 is the Makefile
 -rw-R & lt - r-- . 1 Dingxin Dingxin   189 . 3 dated   22 is  . 17 : 39 max.c
 -RW- R & lt - r-- . 1 Dingxin Dingxin 1280 . 3 dated   22 is  18 is : 33 is max.o
 -rw-R & lt - r-- . 1 Dingxin Dingxin   189 . 3 dated   22 is  18 is : 05 min.c
 -rw-R & lt - r-- 1Dingxin Dingxin 1280 . 3 dated   22 is  18 is : 33 is min.o
 -rwxr-X-XR . 1 Dingxin Dingxin 8640 . 3 dated   22 is  . 19 : 07 testcb
 -rw-R & lt - r-- . 1 Dingxin Dingxin   572 . 3 dated   22 is  18 is : 08 testcb. C
 -rw-R & lt - r-- . 1 Dingxin Dingxin 2720. . 3 dated   22 is  18 is : 33 is testcb.o

Initial use of the library, can only be picked up the pace after the teacher, rarely have their own innovations and hopes to reach more advanced knowledge to enrich themselves

Guess you like

Origin www.cnblogs.com/Dreamer12135/p/12547840.html