First of all, What is the link?
Compile each source code module independently and then “assemble” them as required. The process of assembling the module is a link.
Links are divided into static links and dynamic links. The link to the function library completed at compile time is a static link. All related object files and related function libraries are linked to form an executable file. When the program is running, it has no connection with the function library, because all the required funcions are copied to the relevant location. These libraries are called static libraries and are usually in the form of a file named “libxxx.a”.
Next, use an example to demonstrate how to compile and use a static link library.
Suppose there are five such files: add.h, add.cpp, sub.h, sub.cpp, and main.cpp. The code is as follows.
- add.h
1 |
|
- add.cpp
1 |
|
- sub.h
1 |
|
- sub.cpp
1 |
|
- main.cpp
1 |
|
First compile add.cpp and sub.cpp into .o file with the following code:
1
2g++ -c add.cpp
g++ -c sub.cppGenerate the file add.o sub.o. The compile option of -c means that it only executes to compile and output the target file, as shown in the following figure.
Both static and dynamic library files are created from .o files.
Create a static library (.a file) from the .o file and execute the command: ar cr libmymath.a sub.o add.o to generate the libmymath.a file.
The naming convention for library files begins with lib (prefix) followed by the name of the static library with the suffix .a.
r option for the ar command: Insert the module into the library.
c option for the ar command: Create a library. It will be created regardless of whether the library exists or not.
tv option for the ar command: Display which target files are in the library file, and display detailed information such as file name, time, and size.
Use a static library in the program, execute the command
g++ -o main main.cpp -L. -lmymath
, will generate the main file.
In addition to static linking, it is also possible to defer the loading of links to some library functions to the runtime of the program. This is the dynamic link library technique. The dynamic library file extension is .so. The following example demonstrates how to compile and use dynamic link libraries.
Or through the above code, type the following command to get the dynamic library file libmamath.so.
1 | g++ -fPIC -o add.o -c add.cpp |
or
g++ -fPIC -shared -o libmymath.so add.cpp sub.cpp
-fPIC: Indicates that it is complied into a position-independent code
-Lpath: Indicates to search the library file in the path directory, such as -L. to indicate the current directory.
When the static library file and the dynamic library file have the same name, the compiler will first search for the libxxx.so file in the path directory. If it is not found, coutinue to search for libxxx.a
Dynamic link libraries facilitate inter-process resource sharing.
The static library is faster when the program is executed. Because dynamic library functions must be loaded at runtime.
REFERENCE:
《后台开发:核心技术与应用实践》 徐晓鑫