Implementation and comparison of static and dynamic library under linux

First, create a folder and change the file Xiacun into the following folder:

cal.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



add.c

#include"cal.h"
int add(int var1, int var2){
    return var1 + var2;

}

div.c

#include"cal.h"
int div(int var1,int var2){

    return var1 / var2;
}

mod.c

#include"cal.h"
int mod(int var1, int var2){
    return var1 % var2;
}

mul.c

#include"cal.h"

int mul(int var1,int var2){
    return var1 * var2;
}

sub.c

#include"cal.h"

int sub(int var1,int var2){
    return var1 - var2;
}

testcal.c

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

int main(int argc, char **argv)
{
    int var1;
    int var2;
    printf("please input var1:");
    scanf("%d",&var1);
    printf("please input var2:");
    scanf("%d",&var2);

    printf("%d add %d is %d\n",var1, var2, add(var1,var2));
    printf("%d sub %d is %d\n",var1, var2, sub(var1,var2));
    printf("%d mul %d is %d\n",var1, var2, mul(var1,var2));
    printf("%d div %d is %d\n",var1, var2, div(var1,var2));
    printf("%d mod %d is %d\n",var1, var2, mod(var1,var2));

    return 0;

}

 

 

A: not achieved library, start by writing makeflie file:

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

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

Makeflie first explain the most important documents of the second and third line of code: testcal representing the target file to be generated, the colon on the right indicates the file you want to generate dependent files, $ (OBJ) represents OBJ speak in the variable taken out.

And then make the input operation, the following display:

cc    -c -o testcal.o testcal.c
cc    -c -o add.o add.c
cc    -c -o sub.o sub.c
cc    -c -o mul.o mul.c
cc    -c -o div.o div.c
cc    -c -o mod.o mod.c
gcc testcal.o add.o sub.o mul.o div.o mod.o -o testcal

At the same time enter ls -l can view file size

total 48 
-rw-rw-RW- 1 Marxism Marxism   236 Mar 20  22 : 17 Makefile
 -rw-rw-RW- 1 Marxism Marxism    69 Mar 20  20 : 52 add.c
 -rw-rw-RW- 1 Marxism Marxism 1232 Mar 21  16 : 55 add.o
 -rw-rw-RW- 1 Marxism Marxism   127 Mar 20  20 : 55 cal.h
 -rw-rw-RW- 1 Marxism Marxism    68Mar 20  20 : 51 div.c
 -rw-rw-RW- 1 Marxism Marxism 1232 Mar 21  16 : 55 div.o 
drwxrwxrwx 1 Marxism Marxism 4096 Mar 21  14 : 24 libsamp 
drwxrwxrwx 1 Marxism Marxism 4096 Mar 21  15 : 47 libso
 - -rw-rw RW- 1 Marxism Marxism 6580 Mar 21  13 : 57 libzds.a
 -rw-rw-RW-1 Marxism Marxism    68 Mar 20  21 : 09 mod.c
 -rw-rw-RW- 1 Marxism Marxism 1240 Mar 21  16 : 55 mod.o
 -rw-rw-RW- 1 Marxism Marxism    68 Mar 20  20 : 42 mul.c
 -rw--rw RW- 1 Marxism Marxism 1232 Mar 21  16 : 55 mul.o
 -rw-rw-RW- 1 Marxism Marxism    68 Mar 20  20 :40 sub.c
 -rw-rw-RW- 1 Marxism Marxism 1232 Mar 21  16 : 55 sub.o
 -rwxrwxrwx 1 Marxism Marxism 8696 Mar 21  16 : 55 testcal
 -rw-rw-RW- 1 Marxism Marxism   496 Mar 20  21 : 07 testcal.c
 -rw-rw-RW- 1 Marxism Marxism 2736 Mar 21  16 : 55 testcal.o

You can see testcal executable files only less than 9k

And then run the testcal see the results:

marxism@LAPTOP-8JKP8UGI:~/zds$ ./testcal
please input var1:4
please input var2:5
4 add 5 is 9
4 sub 5 is -1
4 mul 5 is 20
4 div 5 is 0
4 mod 5 is 4
marxism@LAPTOP-8JKP8UGI:~/zds$

Visible, it can successfully compile and run.

 

 

Two: through static library

You must first declare it in the next operation, my library file names are zds, first enter the following code:

RCS air libzds.a add.o sub.o mul.o div.o mod.o

In this line of code, libzds.a in the lib prefix, zds you want to generate static library name, .a suffix.

We can and header files and .a .c file executable files into a folder in ease of operation

And enter the following code to generate an executable file named testcal of:

gcc -o testcal testcal.c -static -L. -lzds

Which, testcal for the final executable file, testcal.c said to generate testcal want to rely on documents, but also pay attention to here is the following -L behind., Do not miss.

Enter ./testcal

marxism@LAPTOP-8JKP8UGI:~/zds/libsamp$ ./testcal
please input var1:2
please input var2:3
2 add 3 is 5
2 sub 3 is -1
2 mul 3 is 6
2 div 3 is 0
2 mod 3 is 2

Visible, testcal can be compiled to run at this time to look at the size of the testcal:

-rw-rw-rw- 1 marxism marxism    127 Mar 21 14:01 cal.h
-rw-rw-rw- 1 marxism marxism   6580 Mar 21 14:01 libzds.a
-rwxrwxrwx 1 marxism marxism 958552 Mar 21 14:24 testcal
-rw-rw-rw- 1 marxism marxism    496 Mar 21 14:01 testcal.c

At this size testcal has 958k, seen very much larger than the original. This is a drawback static library, too bulky. But the advantages of static library are some exceptions, such as its adaptable, and is a good link to the file.

 

Three: dynamic library

Create a folder facilitate our operations, and then all of .o, testcal.c and cal.h into this folder

First, run the following command:

gcc -shared -fPIC -o libzds.so add.o sub.o mul.o div.o mod.o

Which, libzds.so in the lib as a prefix, zds dynamic library name, .so dynamic library suffix. This will generate a dynamic library named in zds

Then enter the following command:

gcc -o testcal testcal.c -L. -lzds

To generate an executable file named testcal, but when I enter ./testcal but found it impossible to compile, then enter the following command:

export LD_LIBRARY_PATH=$(pwd)

You can solve the problem, which is then input ./testcal:

./testcal: error while loading shared libraries: libzds.so: cannot open shared object file: No such file or directory
marxism@LAPTOP-8JKP8UGI:~/zds/libso$ export LD_LIBRARY_PATH=$(pwd)
marxism@LAPTOP-8JKP8UGI:~/zds/libso$ ./testcal
please input var1:4
please input var2:3
4 add 3 is 7
4 sub 3 is 1
4 mul 3 is 12
4 div 3 is 1
4 mod 3 is 1

Visible, you can run successfully, then look at the file size

-rw--rw RW- 1 Marxism Marxism 1232 Mar 21  15 : 07 add.o
 -rw-rw-RW- 1 Marxism Marxism   127 Mar 21  15 : 07 cal.h
 -rw-rw-RW- 1 Marxism Marxism 1232 Mar 21  15 : 07 div.o
 -rwxrwxrwx 1 Marxism Marxism 7664 Mar 21  15 : 45 libzds.so
 -rw-rw-RW- 1 Marxism Marxism 1240 Mar 21 15 : 07 mod.o
 -rw-rw-RW- 1 Marxism Marxism 1232 Mar 21  15 : 07 mul.o
 -rw-rw-RW- 1 Marxism Marxism 1232 Mar 21  15 : 07 sub.o
 -rwxrwxrwx 1 Marxism Marxism 8544 Mar 21  17 : 30 testcal
 -rw-rw-RW- 1 Marxism Marxism   496 Mar 21  15 : 07 testcal.c
 -rw-rw-RW- 1 marxism marxism 2736 Mar 21 15:43 testcal.o

You can see, testcal file only 8k, compared to a static library, dynamic library generated executable file is much smaller, but also some disadvantages of dynamic libraries, for example, relatively poor adaptability, prone to problems.

 

In summary, the static library files generated adaptable, but too bulky. Dynamic libraries generated files and the original head executable files similar, but relatively poor adaptability.

In selecting these two libraries to choose according to their needs.

Guess you like

Origin www.cnblogs.com/deshunzhu/p/12540534.html