Table of Contents
How to write a library
static library
Introduction — static library principle
form static library
introduce
ar
Options
Create static library
Add files to existing static library
After we have a library, how should we give it to others?
dynamic library
The difference between static library and dynamic library linking
static library
dynamic library
Shared by multiple executable files
create
-fPIC option –compile
-shared option — package dynamic library
How to use the library
static library
Library installation
introduce
File default search path
Copy your own files to the system path
question
Solution — -l + library name
Specify the file path to use
introduce
-I — Search for header files
-L — Search for library files
dynamic library
System installation
gcc specifies path (half the battle)
ldd command
How to link a static library when both libraries exist
-static
question
analyze
Solution — Change LD_LIBRARY_PATH
introduce
use
Configure /etc/ld.so.conf.d/
introduce
use
ldconfig — update configuration file
Create soft link
Why have a library?
user
The person who wrote the library
How to write a library
static library
Introduction — static library principle
First, write two programs, and they have their own independent header files
If we directly give the *.o and *.h files to someone else, can that person use it successfully?
After generating main.o, there are *.o files and *.h files in the directory
Directly link all .o files and successfully generate my.exe:
You can see!! This way can make the program run successfully!!!
Therefore, only .o and .h can form executable files!
And this is the principle of static libraries
Form static library
Introduction
However, it is too frustrating to give others a bunch of .o files, so you can package them and then send them to others.
The process of packaging .o files is called forming a static library
We can write such a series of operations into the makefile
ar
Commands for creating and managing static library (archive) files
options
c (create): Used to create a new static library file. If the specified library file already exists, it will be overwritten.
r (replace): Used to add target files to an existing static library. If the library file already contains the target files to be added, they will be replaced.
t (table of contents): Used to list the names of object files contained in a static library
x (extract): Used to extract the specified target file from the static library and copy it to the current directory
d(delete): Used to delete the specified target file from the static library
q (quickly append): Used to quickly append object files to existing static libraries, usually faster than the “r” option
u(update): Used to add an object file to a static library only if the object file does not exist in the library
s (write an object-file index into the archive): Create or update an index of static library files to improve retrieval speed
A (don’t rebuild the archive): Normally, the “ar” command rebuilds static libraries, but using the “A” option prevents it from rebuilding the library files. This is often used to speed up operations.
v (verbose): Run the “ar” command in verbose mode, showing details of each operation, such as adding, deleting, or extracting files
Create static library
ar cr libmylibrary.a file1.o file2.o file3.o
Add files to existing static library
ar rs libmylibrary.a new_file.o
When we have the library, how should we give it to others?
First, we must ensure that both the .h file and the .o file are packaged for others
So you can create two folders to store two types of files:
Put the .a file (after packaging the .o file) into lib, and the .h file into include
After a series of operations, mylib is created:
(This way we have our own library)
Dynamic Library
The difference between static library and dynamic library linking
Static library
- When compiling an executable file, the compiler embeds the object code of the static library directly into the executable file
- It completes address allocation and linking during the linking phase (that is, absolute addressing), so its size will be larger
- Each executable contains a copy of the static library
- Therefore they are independent of each other in memory and do not share the same instance.
Dynamic library
- Dynamic libraries are loaded at runtime into the address space of the executable file (in the shared area)
- During symbol resolution, when a symbol reference is encountered, the dynamic linker will find whether there is a corresponding symbol definition in the dynamic library
- If a symbol definition is found, the dynamic linker will determine the address of the symbol in the dynamic library (using relative addressing), and then assign the address of the found symbol definition to the corresponding symbol Reference(Mapped via page table)
Shared by multiple executable files
- It is usually loaded into the address space in the form of shared memory
- (There is actually only one copy in the memory, but it is mapped to different address spaces through different page tables) to save memory:
Create
It needs to be different from ordinary compilation when forming the .o file
-fPIC option — compile
Generate .o file
-shared option — package dynamic library
The static library is packaged using the ar tool, but the dynamic library is packaged using the gcc + -shared option
Others are almost the same as static libraries.
How to use the library
Static library
Library installation
Introduction
Copying the library to the default path of the system is called library installation.
In fact, installation is copying
However, generally Do not add your own libraries to the system library files
- Unreliable, unsafe, not officially tested
- After a long time, you may forget to add the library you wrote to the system, and you may accidentally use the library you wrote.
File default search path
Copy your own files to the system path
Copy your own packaged header files/library files to the system path:
Question
After compiling main.c, I found that it still couldn’t be compiled.
It says that these function symbols cannot be found:
- It’s because the library files we wrote ourselves are third-party libraries
- You need to specify the name of the library you want to link when compiling (because there are many system library files, you need to specify which one)
Solution — -l + library name
Used to specify library files
Library name — Remove the lib prefix and the remaining part after removing the .a suffix
This completes the compilation and our program can be run:
Specify the file path used
Introduction
Copying files directly into system files is really risky
So we can manually specify the path without adding it to the system path
-I — Search for header files
Search under the specified path:
-L — Search for library files
Search for library files in the specified path, and must specify which library it is:
In this way, we successfully compiled a.out
Dynamic library
System installation
the same as above
gcc specified path (half the success)
When there are static libraries and dynamic libraries with the same name in our lib, the dynamic library will be used by default:
ldd command
Used to list dynamic link dependencies of an executable or shared library
When both libraries exist, How to link a static library h5>
-static
The file we generated can be executed successfully
Question
If not specified, use dynamic library for linking
It will be displayed that this executable file cannot find this dynamic library:
Linking static libraries can run successfully, so why can’t dynamic libraries?
Analysis
- The reason is that dynamic libraries are linkedat runtime
- Running this process is not under the control of the compiler.
- If you tell gcc where the dynamic library is, it can only allow it to compile successfully, but not execute it
- And links require the loader in os to work
- So we need to tell the loader relevant information!!!
- That is the following way
Solution — Change LD_LIBRARY_PATH
Introduction
It is an environment variable, used to specify the search path of the dynamic link library, to let the loader know where the library it should link to is
But!!! This environment variable only takes effect for the current session and its sub-processes
use
Set this environment variable through export (add the dynamic library path you implemented)
At this time we can successfully execute:
However, setting environment variables can only take effect in this conversation, and will not happen next time you restart.
So we need to use another once and for all method
Configuration/etc/ld.so.conf.d/
Introduction
- It contains the library path configuration file for the dynamic linker
- Each file in this directory is a text file, suffixed with .conf, that specifies the path to a library file to be searched by the dynamic linker
- Each line in these files contains a path that the dynamic linker will search for the required shared library files in the order specified in the files
- When we install some custom library files or third-party software in the system, we can create a new .conf file and add the path to it so that the dynamic linker can find these library files.
Use
Since it is a system configuration file, root permission is required to create files in it.
In addition, sudo is required to modify the file content.
Write our dynamic library path into mylib.conf:
When we re-execute, we will find that our dynamic library is still not found at this time? Haven’t we already set it up?
ldconfig — update configuration file
- After creating or modifying files in the /etc/ld.so.conf.d/ directory, you need to use this command to update the cache of the dynamic linker
- This command will re-read the configuration file and update the dynamic linker’s cache so that it can correctly find the new library file path.
Although we have added it, it must be updated before the linker can get the new path.
Including the files we will delete, they also need to be updated:
After the update, use the ldd command and the dynamic library used by a.out can be found!
Of course there is no problem with execution!!!
Create soft link
In the system library file, create a soft link with the third-party library. You can also make the executable file find its dynamic library
This can also be executed successfully:
Our dynamic library can be found at:
Why do we need a library
user
- It can greatly reduce the development cycle and improve the quality of software.
- After all, the released libraries have been strictly tested, and writing your own may not be as safe.
- And once it is changed, it will greatly affect the upper-layer software.
The person who wrote the library
- If you want to share your code with others, it will be very simple to package it into a library and send it (if you give the source code directly, it will be too messy and difficult to find)
- What is given is usually compiled files rather than source code, so others cannot steal your own code (code security + guarantee your own patents)
-static
The file we generated can be executed successfully
Question
If not specified, use dynamic library for linking
It will be displayed that this executable file cannot find this dynamic library:
Linking static libraries can run successfully, so why can’t dynamic libraries?
Analysis
- The reason is that dynamic libraries are linkedat runtime
- Running this process is not under the control of the compiler.
- If you tell gcc where the dynamic library is, it can only allow it to compile successfully, but not execute it
- And links require the loader in os to work
- So we need to tell the loader relevant information!!!
- That is the following way
Introduction
It is an environment variable, used to specify the search path of the dynamic link library, to let the loader know where the library it should link to is
But!!! This environment variable only takes effect for the current session and its sub-processes
use
Set this environment variable through export (add the dynamic library path you implemented)
At this time we can successfully execute:
However, setting environment variables can only take effect in this conversation, and will not happen next time you restart.
So we need to use another once and for all method
Introduction
- It contains the library path configuration file for the dynamic linker
- Each file in this directory is a text file, suffixed with .conf, that specifies the path to a library file to be searched by the dynamic linker
- Each line in these files contains a path that the dynamic linker will search for the required shared library files in the order specified in the files
- When we install some custom library files or third-party software in the system, we can create a new .conf file and add the path to it so that the dynamic linker can find these library files.
Use
Since it is a system configuration file, root permission is required to create files in it.
In addition, sudo is required to modify the file content.
Write our dynamic library path into mylib.conf:
When we re-execute, we will find that our dynamic library is still not found at this time? Haven’t we already set it up?
ldconfig — update configuration file
- After creating or modifying files in the /etc/ld.so.conf.d/ directory, you need to use this command to update the cache of the dynamic linker
- This command will re-read the configuration file and update the dynamic linker’s cache so that it can correctly find the new library file path.
Although we have added it, it must be updated before the linker can get the new path.
Including the files we will delete, they also need to be updated:
After the update, use the ldd command and the dynamic library used by a.out can be found!
Of course there is no problem with execution!!!
In the system library file, create a soft link with the third-party library. You can also make the executable file find its dynamic library
This can also be executed successfully:
Our dynamic library can be found at:
user
- It can greatly reduce the development cycle and improve the quality of software.
- After all, the released libraries have been strictly tested, and writing your own may not be as safe.
- And once it is changed, it will greatly affect the upper-layer software.
The person who wrote the library
- If you want to share your code with others, it will be very simple to package it into a library and send it (if you give the source code directly, it will be too messy and difficult to find)
- What is given is usually compiled files rather than source code, so others cannot steal your own code (code security + guarantee your own patents)