[Note: The operations in this article are performed on the ubuntu-10.04.4.]
Static library and dynamic library
Essentially, a library is a binary form of executable code that can be loaded into the memory for execution by the operating system. o file (target file) created.
- Static Library: the code of the static library has been loaded into executable programs during compilation, so the program size is large.
- Dynamic library: If different applications call the same library, only one dynamic library is required in the memory. Upgrading or replacing a single dynamic library does not affect the normal loading and use of other dynamic libraries, so it is easy to upgrade.
Compile multiple source files
1. Create hello. C:
#include<stdio.h>void hello(){ printf(“Hello world!\n”);}
Run the GCC-C hello. C command to generate the target file hello. O.
2. Create hello. h:
#ifndef _HELLO_#define _HELLO_extern voidhello();#endif //_HELLO_
3. Create main. C:
#include “hello.h”int main(intargc, char **argv){ hello(); return 0;}
4. Execute the command gcc-C main to generate the target file main. o
5. Compile hello. O and Main. O in combination to generate the executable file Hello: gcc-O hello. O main. O.
Note: the size of the executable file Hello generated by multi-file compilation is 7184b.
Static library generation and reference
1. Execute the command gcc-C hello. C to generate the target file hello. O;
2. Run the ar rcs libhello. A hello. O command to generate the static library file libhello.;
3. Load the static file gcc-O hello main. C-static-L.-lhello during compilation to generate the executable file hello.
Note: the size of the executable file "hello" generated by the static library is 594740b, and the size of the static library "libhello. A" is 982b. After deleting the static library, "hello" can still be executed. It can be seen that the executable files for loading static databases are relatively large.
Dynamic library generation and reference
1. Execute the command gcc-C hello. C to generate the target file hello. O;
2. Run the GCC-shared-FPIC-O libhello. So hello. O command to generate the dynamic library libhello. So;
3. Run the GCC-O hello main. C-L.-lhello command to generate the executable file hello.
When you run Hello: $./Hello, an error is returned:
./Hello: errorwhile loading shared libraries: libhello. So: cannot open shared object file: nosuch file or director
4. To execute hello, you need to link to the dynamic library. The method is as follows:
A) Copy libhello. So to the/usr/lib directory;
B) Export LD_LIBRARY_PATH = $ (PWD), add the directory (current directory) Where libhello. So is located to LD_LIBRARY_PATH;
C) chcon-T texrel_shlib_t/usr/lib/libhello. So, the absolute path of the Shared Library ;[? The role of this command is unknown]
Note: the size of the executable file Hello referencing the dynamic library is 7128b, and the dynamic library libhello. the so size is 6734b. Compared with compiling multiple source files to generate the hello size 7184b, it can be seen that loading the dynamic library will not cause the executable file to become larger. To ensure that hello can be successfully executed, you must ensure that the application can be linked to the dynamic library. After verification, only 4.a) or 4.bcan be completed.
How does the linker Load dynamic databases?
The order in which the linker searches for a dynamic database:
1. dt_rpath segment of the ELF file;
2. Environment Variable LD_LIBRARY_PATH;
3./etc/lD. So. cache file list;
4./lib,/usr/lib directory;
You only need to find the dynamic library and load it into the memory.