1. The concept of library files
A library is a collection of precompiled methods (.o files). The location of the library stored in the Linux system is generally: /lib and /usr/lib.
On 64-bit systems some libraries may also be stored under /usr/lib64. The header files of the library are generally stored in /usr/include or its subdirectories.
There are two kinds of libraries, one is a static library, and its command rule is libxxx.a, and the other is a shared library, and its command rule is libxxx.so.
2. Static library and shared library
If both static library and shared library exist, the shared library will be preferred.
1. How to use the static library
Directly include the implementation of the methods in the static library to be used into an executable program main.
【example】
If you have implemented two methods add and max yourself, these two implementations are in a static library file, assuming the library file name is libfoo.a, and then you write a program main.c yourself, and call it in main.c Add method, first compile main.c into main.o binary instruction file. Next comes the linking step. When linking, not only the library libfoo.a that stores the methods implemented by itself is used, but also a copy of the methods in the libfoo.a static library is used for linking. Some other libraries will also be used. First, before executing the main function, we need to execute some codes, which are directly linked from some .o files, and the main function main program will be called in these codes. Therefore, we have to implement a main method as an entry. Second, some standard libraries, such as the C library where the method implemented by printf is located, also need to be linked. The final generated executable program main includes the .c file written by myself and the methods in the linked library.
2. How to use the shared library
It only marks the methods used in the shared library, and does not include the implementation of the methods in the shared library in an executable program main.
【example】
If you implement two methods add and max yourself, these two implementations are in a static library file, assuming the library file name is libfoo.so, and then write a program main.c yourself, call it in main.c Add method, first compile main.c into main.o binary instruction file. Next comes the linking step. When linking, first, a part of code will be executed before the main function is executed. These codes are linked from some .o files. Second, we will link standard libraries like C library. , Third, mark the add method used in the shared library.
Now there is a simple main program:
Check the shared library of this main program and find that the C library will be linked by default, and libc.so is the C library:
3. Advantages and disadvantages of static libraries and shared libraries
(1) Static library
Advantages:
Include the implementation of the methods in the used library into its own executable program, which has become a part of the executable program, and will not search for the used library when running, and delete this library No effect either. Include all the libraries used in its own executable program, regardless of whether there is a corresponding library program in this environment, it can be executed.
Disadvantages:
① It takes up a lot of memory;
② It is inconvenient to update the library, and the program must be recompiled when updating the library.
(2) Shared library
Advantages:
① Small memory occupation;
② Library update is very convenient, just overwrite the old library with the new library
Disadvantages:
not included, you need to find the library to be used at runtime, and dynamically link the corresponding library, so all shared libraries that need to be used must exist on the computer that executes the program, once the corresponding library does not exist , the program cannot run.
3. Generation and use of static libraries
header files are in/usr/include
The library files are all in /usr/lib
or/lib
Executables are /usr/bin
either/bin
1. Steps to generate a static library:
Example: Currently there is an add.c and max.c file, both add.c and max.c are declared in a header file foo.h:
(1) In the first step, compile all .c files into .o files:
(2) In the second step, pack the .o files into a library, and use the ar command to generate a static library for all the ".o" files compiled in the first step
The following add.o max.o
files are generated as a static library libfoo.a
:
For ar crv libfoo.a add.o max.o
the
ar
is a command
crv
In:
c is to create the library
r is to add the method to the library
v is to show the process
libfoo.a
is the name of the library created
add.o max.o
is the .o file that will be packaged into the library
2. Use of static libraries
(1) First create a directory liba
Copy both the header file foo.h and the static library libfoo.a to this directory:
(2) Create a main.c file and write test code
(3) Generate an executable file and run it
① Directly compile the main.c file (failure)
Reason: There is no implementation of the add method in this directory.
② Generate the main.o file and then compile the main.c file (failure)
Failure reason: gcc -o main main.o
The add method could not be found at link time
③ When linking, specify a static library to generate an executable file (success)
④ Generate executable files directly through main.c and static libraries (success)
gcc -o main main.c -L -lfoo
in the compile statement
-L
: Specify the storage path of the library
-l
: Specify the name of the library (the preceding 'lib' and the extension '.a' are not required).
If the library is in the standard library, there is no need to specify the path to the library at compile time.
(4) After deleting the static library libfoo.a, the executable file main can still run
Fourth, the generation and use of shared libraries
1. Generation of shared library
1. Steps to generate a shared library:
Example: Currently there is an add.c and max.c file, both add.c and max.c are declared in a header file foo.h:
(1) In the first step, compile all .c files into .o files
(2) In the second step, pack the .o files into a library, and use the gcc command to generate all the ".o" files compiled in the first step into a shared library
gcc -shared -fPIC -o libfoo.so add.o max.o
In the command to generate the shared library :
gcc
Indicates the command
-shared
means that the generated shared library means
-fPIC
that the code position is independent
-o
means that the output
libfoo.so
is the generated shared library. The name of the shared library
add.o max.o
is the .o file to be packaged into the library
2. Use of shared libraries
(1) First create a directory liba
Move both the shared library libfoo.so and the header file foo.h into liba:
(2) Create a main.c file and write test code
(3) Generate executable files
① Compile directly to generate an executable file (failure)
Reason for failure: The implementation of the add method cannot be found, and there is no implementation of the add method in main.c
② Build executable using shared library "libfoo.so" and "main.c" (success)
In compile statement gcc -o main main.c -L. -lfoo
:
-L
: Specify the storage path of the library
-l
: Specify the name of the library (the preceding 'lib' and extension '.so' are not required)
If there are shared libraries and static libraries with the same name in the storage path of the library, gcc uses the shared library by default.
(4) run (failure)
Analysis of failure reasons:
First, check which libraries are used by the main program:
It is found that libfoo.so is not found. This is because when looking for libraries at runtime, it will only look for them in the standard directory. gcc -o main main.c -L. -lfoo
The path specified in is to tell gcc to go to this path to find the required libraries when compiling.
Solution:
① Use some environment variables to specify that the main program searches for the required library in the current directory when loading the library
First set the variable "LD_LIBRARY_PATH" as the current path, and it will still fail to run at this time:
Then set the variable "LD_LIBRARY_PATH" as an environment variable to run successfully:
②Put the required libraries into the standard directory (operate in administrator mode)
[Note] If the dynamic library is moved to the standard directory and the environment variable is set, then the environment variable will be preferred to find the location of the library.
At this time, it is found that libfoo.so has been found, and the main program can be run at this time:
(5) After deleting the shared library libfoo.b, the executable file main cannot run