Static and dynamic libraries in linux

1.What is a library
   A library is a binary file,that is to say turns source code into binary code.
2.How do users use libraries
    Users need header files and libraries itself.
3.How to use libraries
    3.1 How to name a library: libNAME.a
    3.2 Steps for making a static library:
        1) Compile: from .c to .o
            gcc a.c b.c -c    // The result is generating a.o b.o 
        2):package : with the tool of ar
            ar rcs NAME   raw materials  
            ar rcs libku.a a.o b.o
    3.3 View a static library :with the tool of nm 
        nm libku.a 
    3.4 How a programmer use static libraries
        1):Header file and library are needed to use a library.
        2):grammar of using a library:
            gcc main.c -LPATH_OF_LIB -lNAME
            gcc main.c -L./ -lku
    3.5 How to use multiple libraries at the same time.How to merge multiple libraries into one.
        ar x A.a  //generating A.o
        ar x B.a  //generating B.o
        ar x C.a  //generating C.o
        ar cru libABC.a *.o

4.The use of dynamic libraries
    4.1 How to name a dynamic library
        libNAME.so
    4.2 制作步骤
        1) Compile : from .c to .o
            gcc a.c b.c -c -fpic
        2):package
            gcc -shared -o libNAME.so a.o b.o
    4.3 How to use a dynamic library
        1) Method one:Use a dynamic library like the way of using a static library
            gcc main.c -L./ -lku -omain
            But the following error is reported when running ./main
                ./main: error while loading shared libraries: libku.so: cannot open shared object file: No such file or directory
            You need to get over the following two problems if you want to resolve the problem above. 
                a) : How related dynamic libraries are searched when running applications in linux.
                   The related dynamic libraries are never searched in current directory in linux which is totally different from Windows.
                    The order of paths which are searched :/lib -> /usr/lib -> directories specified by the environment variable LD_LIBRARY_PATH
                    So we can reslove the problem above in the following way.
                       Copy libku.so to /lib
                       Copy libku.so to /usr/lib
                       Copy libku.so to directory specified by the environment variable LD_LIBRARY_PATH
                       Add the path of libku.so to the environment variable LD_LIBRARY_PATH.
                b) : Dynamic libraries are not the part of application,but the paths of related dynamic libraries have to be specified when link happens,why?.
                       The function name does not exist after the function is compiled,and it will convert into an address,the location of the function must be verified when link happens,so the reason the paths of related dynamic libraries have to be specified when link happens is we need to identity the location of functions(functions are called not according to its name but according to locations).
        2):with the tool of libdl.so,a library built in linux,encapsulating a set of functions which are used to call custom libraries(自定义库)
            The lib is needed in compiling and the header file dlfcn.h is needed in code
                gcc main.c -ldl -omain  //the key thing is -ldl
            How the lib used in code:
                void* handle = dlopen("./libku.so",RTLD_LAZY); //open a lib
                int(*fun)(int,int) = dlsym(handle,"add");      //get the pointer
                int a = fun(53,2);                             //using function
                dlclose(handle);                               //close lib
                
5.Merits and demerits of static and dynamic libraries
    5.1 Merits and demerits of static libraries
        Merits :They load quickly because they are packaged into application.
               You don't need to provede static libraries when you publish your application,transplantation is convenient.
        Demerits : waste of memory
    5.2 Merits and demerits of dynamic libraries
        
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

猜你喜欢

转载自blog.csdn.net/liujun5319/article/details/82008814