NBasic Framework:
A) use of external Libraries
B) differences between static and shared libraries
C) static library generation
D) generation of shared libraries
2.1Use of External library
2.1.1 database file features:
1) library files are actually packaged with some. O files;
2) It is written in accordance with the reusable principle;
3) it is usually composed of a group of interrelated functions;
4) The software has good maintainability. If the software is upgraded, you only need to modify the library code instead of modifying the code of the database (as long as the library interface is not changed ).
2.2.2 system storage location of header files and library files
Header file:/usr/include/usr/local/include
Library File:/usr/lib/usr/local/lib
2.2.3 use of external Libraries
-Lname indicates that you want to link libname. So or libname. A library file.
If both the static and shared libraries exist, the system preferentially links the shared libraries.
2.2Differences between static and shared libraries
Static Library (. a) features:
1) when linking the program, link the corresponding function code in the required Library to the executable file;
2) The static library is no longer needed during running;
3) if multiple programs use the same database, multiple copies of the relevant code in the database are required, which consumes a lot of memory and disk space.
Shared Library (. So) features:
1) when the program runs, the operating system loads the library code from the disk to the memory. This process is called a dynamic link;
2) The shared library must exist during running;
3) if multiple programs use the same shared library, when the program is running, only one copy of the library code needs to be loaded into the memory, and multiple programs share the same database;
Because the executable program only contains a table of the function entry address it uses, rather than copying the library code;
Therefore, memory and disk space are saved;
4) because the executable program does not copy the library code, the maintenance performance is good.
2.3Static library generation
2.3.1 static library generation
Ar RCS libname. A name. o
Indicates (replace, create, save)
* Ar t queries the number of. O files in a static library.
2.3.2 database search path:
1) Search for the directory specified by-I-l from left to right ;(It is best to use1MethodTo set)
2) directory specified by Environment Variables
Configuration method: To the configuration file (~ /. Bash_profile) to add the search path to the corresponding environment variables:
Export c_include_path = ......
Export cpp_include_path = ......
Export LIBRARY_PATH = ......
Then execute the configuration file.
3) default search directory
/Usr/include/usr/lib
2.4Share library generation
2.4.1 generation of shared libraries
Gcc-shared-FPIC hello. O-o libhello. So
FPIC: used to generate position independent code)
2.4.2 configuration required for running the Shared Library (two methods)
1) copy the. So file to the system shared library path:/usr/lib
2) set the environment variable LD_LIBRARY_PATH (The second method is recommended.)
L LDD + Executable File Name: query the shared libraries to be loaded when the program is executed.