What is a link library | Dynamic library and static library

insert image description here

Welcome to follow the blogger Mindtechnist or join [ Linux C/C++/Python Community ] to learn and share Linux, C, C++, Python, Matlab, robot motion control, multi-robot collaboration, intelligent optimization algorithm, filter estimation, multi-sensor information fusion, Knowledge and technology in machine learning, artificial intelligence and other related fields.



Column: Common problems and solutions of development tools


what is link library

Sometimes when we provide functions to the outside world, we may not want the other party to see the source code, so we can make a library file, and give the library file and header file to the other party to achieve the purpose of providing functions without exposing the source code. The link library refers to compiling the library file and packaging it into a binary file, and these binary files will be loaded into the memory when the program is called. In fact, after one or more source files are compiled into object files, the external symbols referenced in this file need to be linked to find the missing addresses. There are two ways of linking. If all the linking operations have been completed before the executable file is generated, this link is called static linking, and this kind of library file is called static linking library; if it is in the program The link is only performed when it is executed. This is called a dynamic link, and the corresponding library file is called a dynamic link library. Because of this, the executable file generated when using the static library can run independently, because it no longer needs external content, and the executable file generated by compiling the dynamic library cannot run independently, because it is only at runtime. Will go to the external address referenced by the link.

static library

The static library will be directly loaded into the code segment, and it will be linked with all object files into an executable file, which can run independently after the executable file is generated. However, just because the static library will be directly loaded into the code segment of the memory, all the instructions and data of the object file and the static library are copied inside the executable file, and the executable file generated by compilation will be relatively large. Moreover, if there are multiple executable files linked to the unified static library in the entire system, each executable file must copy a copy of the instructions and data of the static library, which causes a waste of space, because the data they copy are same content. Finally, once the code of the static library file is updated, it is necessary to recompile and link to regenerate the entire executable file, which is troublesome to update and upgrade. In the Linux system, the name of the static link library file is usually libxxx.a, and in the Windows system, the suffix of the static link library file is .lib.

dynamic library

In fact, the term dynamic library itself is a name for the library files used for dynamic linking on the Windows platform. Under Linux, it is generally called a shared library. A dynamic library is a shared library segment loaded into memory at runtime. In this way, if many programs need to use a static library, it will save a lot of memory, because it is not loaded into the code segment like a static library, but in The shared library segment that is loaded into memory at runtime. When multiple programs use the same dynamic library, all programs can share the instructions and data of this shared library segment. The implementation of dynamic linking is like this. When compiling, the static linker first links all the object files into an executable file. When the program is running, the dynamic library to be used will be loaded into the shared library segment of the memory. Dynamic linking The compiler completes the linking work of the executable file and the dynamic library file, which can be understood as loading the memory on demand (the memory will only be loaded when it is needed). The dynamic library greatly facilitates the upgrade and change of the program. Just replace the old dynamic library file with the new dynamic library file, and the new library file will be automatically connected at runtime. However, because of the feature of dynamic library loading at runtime, executable files using dynamic libraries will be slightly slower at runtime, but overall, the performance loss of running speed is far less than the benefits of memory saving. In the Linux system, the name of the dynamic link library is usually libxxx.so, and in the Windows system, the suffix of the dynamic link library is .dll. When the GCC compiler generates an executable file, it will give priority to using the dynamic link library to complete the link by default. If there is no dynamic link library required by the program file in the current system environment, GCC will select the static link library for static linking. If neither library file is found, linking fails.

header files and library files

When we release the library file, we need to release the library file and the header file together. The header file stores the declaration part of these functional modules such as variables, functions or classes, and the library file stores the specific implementation part of each module. That is to say, the interface for calling the function modules in the library file is defined in the header file. The existence of the header file also realizes such a function. When we provide the function to the outside world, we can hide the source code through the library file. The user of the function only needs to call the function module according to the interface provided by the header file.

Manually add the linked library

When using GCC to compile and link programs, GCC will link libc.a or libc.so by default, but for other libraries (such as non-standard libraries, third-party libraries, etc.), you need to add them manually. Use the GCC -I option to specify the library name, just add the library name after -I.

Under normal circumstances, when we specify the library name to be used, GCC will automatically search for files in the standard library directory, such as /usr/lib. However, if we want to link libraries located in other directories, such as our own library, or we want to reference other people's libraries, we need to display the path of the specified library when compiling.
① Just like specifying the path of a common header file, specify the full path and file name of the library file for GCC display.

 gcc main.c -o main.out -I /usr/lib/libm.a

② Use the -L option of GCC to add a search directory for GCC: you can use multiple -L options, or use a colon: split within an option to specify multiple search paths.

③ Add the directory where the library file is located to the environment variable LIBRARYPATH.


insert image description here
insert image description here


Guess you like

Origin blog.csdn.net/qq_43471489/article/details/130398892