Static library (Archives)
- The suffix is generally "*.a"
- Create a compile-and-link command for a single static library using two target files: ar cr libtest.a test1.o test2.o
- Link all referenced and unhandled symbols when the linker searches for a static library
- Place a link to a static library at the end of the command line to ensure that its references are parsed correctly
Dynamic library (Shared Object)
- Shared target libraries (Windows-like DLLs) with a suffix of "*.so"
- Compile command: g++-shared-fpic-o libtest.so test1.o test2.o
- PIC: Location independent code (position-independent codes)
- the translator first links the dynamic library, followed by the static library
- If you want to force a link to a static library, compile using the-static option
Standard library and library dependencies
C Standard library: libc
Math Library Standalone: LIBM; When you need to call a mathematical function, explicitly link the math library: g++-O compute COMPUTE.C-LM
C + + standard library: libstdc++
Compile the C++11 program, use g++ -4.8-std=c++11, and for integrated development environments such as Code::Blocks, select the C++11 option in the Compiler Settings dialog box
Correlation of libraries
- When linking, be aware that cross-references are parsed correctly, for example: Libtiff libraries require libjpeg libraries (JPEG image processing) and LIBZ libraries (compression processing)
- Standalone Library Links: g++-static-o tifftest Tifftest.c-ltiff-ljpeg-lz
- Related Library Links: g++-o app App.o-la-lb-la (cross-correlation between libraries)
Loading and unloading of dynamic libraries
Dynamic library loading Dlopen (): Header file: "Dlfcn.h"
function lookup and load function Dlsym ()
Dynamic library unload function: Dlclose ()
Dynamic Library error handling function Dlerror ()
Call a function in the dynamic library, set the function name to G
The C function should encapsulate extern "C" {...} when mixing C/D + + encoding. block, ensure that name resolution works correctly (C does not support function overloading)
Link Options: "-ldl"
void *handle = Dlopen ("libtest.so", Rtld_lazy);
Declaring a function pointer to a function in a dynamic library, finding it by the name of the called function
void (*test) () = Dlsym (handle, "G");
(*test) ()//Use function pointers to invoke functions in the dynamic library
Dlclose (handle);
C + + Learning Note 23: Libraries