static library and dynamic library

What is a library?
A library is essentially a binary format of executable code that can be loaded into memory for execution. Libraries can be divided into static libraries and dynamic libraries . The difference between the two is that the code is different at different times .

Static library : The name of this type of library is generally libxxx.a, and xxx is the name of the library. The static library will be linked to the target code when the program is compiled . Since all the data of the entire function library will be integrated into the target code, so The file compiled by the static function library will be relatively large. Obviously, its advantage is that it does not require the support of the function library after compilation and execution, and the entire function library has been loaded into the object code during compilation. But when the library changes, it also needs to be recompiled .
Dynamic library : The name of this kind of library is generally libxxx.MNso, M is the main version number of the library, N is the minor version number of the library, of course, the version number is not necessary. Compared with the static library, the dynamic library is not loaded into the object code during compilation, and the program will only be called in the function library when the relevant function is executed, so the executable file generated by the dynamic library is relatively small. Since the function library is not integrated into the program, but the program dynamically applies for calls at runtime, the program's operating environment must provide the corresponding library . The update of the dynamic library will not affect the execution of the program, so it is more convenient to upgrade the functions of the dynamic library .

When a static function library is to be used, the linker will find the functions needed by the program, and then copy them to the executable file. Since this copy is complete, once the linking is successful, the static library is not needed. However, this is not the case with dynamic libraries, which leave a flag in the executing program indicating that the library must be loaded first when the program is executed. Because the dynamic library saves space, the default operation for connection under linux is to connect the dynamic library first.

Static library
-. Generate static library
(1) We assume that there are static libraries st1, st2 to make library libmytest.a. As follows

[root@localhost Testlib]# cat str1.c
#include<stdio.h>

void print1()
{
    printf("i am print1\n");
}
[root@localhost Testlib]# cat str2.c
#include<stdio.h>

void print2()
{
    printf("i am print2\n");
}
[root@localhost Testlib]# cat main.c
#include<stdio.h>

int main( int argc, const char* argv[] )
{
    print1();
    print2();

    return 0;
}

(2) Generate libmytest.a file
Generate libmytest.a, it will generate a lot of .o to .a

[root@localhost Testlib]# gcc -c str1.c str2.c
[root@localhost Testlib]# ar crs libmytest.a str1.o str2.o
[root@localhost Testlib]# ls
libmytest.a  main.c  str1.c  str1.o  str2.c  str2.o
[root@localhost Testlib]# file libmytest.a 
libmytest.a: current ar archive

(3) Use a static library

[root@localhost Testlib]# gcc -o test main.c -L. -lmytest
[root@localhost Testlib]# ./test
i am print1
i am print2

The parameter -L of gcc is to tell the compiler that the library file is the current directory, and -l is to tell the compiler that the name of the library to be used is mytest

Dynamic library
Basic concepts of dynamic library:
1. Dynamic link library is a library loaded when the program is running. When the dynamic link library is installed correctly, all programs can use the dynamic library to run the program. A dynamic link library is a collection of object files, and the organization of object files in the dynamic link library is formed in a special way. The addresses of functions and variables in the library are relative addresses, not absolute addresses, and their real addresses are formed when the program that calls the dynamic library is loaded.
2. The name of the dynamic link library has an alias (soname), a real name (realname) and a linker name (linker name). Aliases consist of a prefix lib, followed by the name of the library, followed by a suffix ".so" ("libxxx.so"). The real name is the real name of the dynamic link library, which is usually formed by adding a minor version number, release version, etc. to the base of the alias. In addition, there is a link name, that is, the name of the library used by the program when linking.
3. When the dynamic link library is installed, always copy the file to a certain directory, and then use a soft link to generate an alias. When the library file is updated, just update the soft link.

Generate dynamic library

[root@localhost dylib]# cat main.c
#include "mylib.h"

int main( int argc, const char* argv[] )
{
    print1();
    print2();
    return 0;
}
[root@localhost dylib]# cat mylib.h
#ifndef  __MYLIB_H__
#define  __MYLIB_H__

#include<stdio.h>

void print1();
void print2();

#endif  //__MYLIB_H__
[root@localhost dylib]# cat dy1.c
#include "mylib.h"

void print1()
{
    printf("my first lib\n");
}
[root@localhost dylib]# cat dy2.c
#include "mylib.h"

void print2()
{
    printf("my second lib\n");
}

Use dy1.c and dy2.c to create dynamic libraries

[root@localhost dylib]# gcc -o libmytest.so -fPIC -shared dy1.c dy2.c

-fPIC specifies to generate an address-independent compiler, and -shared specifies to generate a dynamic link
library
. Before using the dynamic library, we need to put it in /lib, because the dynamic link library linked by the program needs to be in the system directory during operation. Down.

[root@localhost dylib]# cp libmytest.so /lib
[root@localhost dylib]# gcc -o test main.c -L. -lmytest
[root@localhost dylib]# ./test
my first lib
my second lib

Of course, you can also add the path of the library
to the

[root@localhost dylib]# export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480199&siteId=291194637