Static Library (. A) and dynamic library (. So)
Static Library: during the compilation process, the code of the library function is directly added to the generated executable program, and the library function is not required during the program running.
All static library objects must be generated through static links. Because the code is in the executable program, it runs faster, but it causes the bloated executable program.
Shared Library (dynamic library): during compilation, the library function information to be used is simply specified in the generated executable program, and the ing relationship is established with the function only when the program is executed. (Interface,. So, does not need to be re-compiled, so the upgrade is fast ).
Create a static Library:
The static function library is a set of target files (*. O. Generally, a function is a. o to avoid waste.
Put multiple. O files into one library.
Procedure:
1. Generate the. o file gcc-C add. C minus. c ------ generate Add. O and minus. o
2. Package ar-CRS libmath. A Add. O minus. o
When GCC is used for compilation: gcc-o exe test. C-L. (PATH)-L math (name)-I ../include/
It is necessary to talk about the AR function: (but these functions are rarely used !)
Purpose: Create and modify database functions, or extract the target file from the database functions.
Main parameters:
-Rs ----- Add the target file to the database and generate the index file.
-DS ----- Delete the target file from the database and generate an index file.
-X ----- extract the target files listed in the list from the library without modifying the library files.
If libmath. A contains Add. O and minus. O, now I want to add another mod. O to libmath.,
Ar-Rs libmath. A mod. O is added to the libmath. A library.
Another example is to extract mod. O from the library. ar-x libmath. A mod. o
If you want to delete mod. O from libmath. A, then ar-Ds libmath. A mod. o
There is an easy-to-ignore sequence problem during GCC Compilation:
The static library cannot be connected to the original program. This is because there are no undefined symbols at the beginning, and the content in the library will not be linked,
Therefore, you should note that the use of the static library (-L option) is written at the end.
Because GCC scanning is sequential:
E U D
All. O sets undefined symbol sets defined symbol sets
If it is all. O but not. A, the sequence does not affect: for example,
Both gcc-O main. o add. O and GCC-o add. O main. O can be compiled.
There is a command to list the information contained in the warehouse: Nm-s libmath.
Create a shared library:
Gcc-shared-FPIC-O libtest. So test1.o test2.o
Compile a program that uses the Shared Library:
Gcc-O Exe-L. (PATH)-ltest test. c
If you use a shared library program, you can search for the library path during runtime:
1. Copy the dynamic library file to/lib or/usr/lib.
$ CP libalg. So/usr/lib or $ CP libalg. So/lib
2. Add the library path (/home/XXX/LIB) in/etc/lD. So. conf, and then execute the ldconfig command to take effect.
3. Change the environment variable LD_LIBRARY_PATH
$ Export LD_LIBRARY_PATH =/home/XXX/lib/
4. Specify the library search path during compilation,
-Wl,-rpath =/home/XXX/lib (it should be the absolute path of the library in the system)
For example: gcc-o exe test. C./libtest. So-wl, rpath, $ pwd
Note: The last two methods are recommended.
Options related to libraries and paths:
-I dir: Find the file to be included in the Dir directory.
-L dir: Find the database to be-l in the Dir directory.
-The name of the lname Linked Library is libname. A or libname. So.
-FPIC or-FPIC generates location-independent target code to construct a shared library ),
This is used to generate a shared library with no location concerns.
-Static prohibit the connection to the shared library. If no, the shared library link is preferred.
-Shared libraries are generated by shared libraries and used when shared libraries are created.