The first two days to understand the dynamic link library and static link library, and finally basically understand that I did not understand the "dlopen" is what happened, how also to take a steak.
Shared library, there are two forms, the first is the "Dynamic link library" mentioned in the previous article, and another form of shared library, it is called "Dynamic Loading Library", that is, I just mentioned the "Dlopen" way to play. Dynamically loading the library at compile time, it should not need to go to-l reference Lib, but in the executable program, you can decide to load the library time. For example, the program ran running, suddenly want to use a libabc.so library called ABC function, then you can use Dlopen to open the library, and then use DLSYM to find ABC function pointer and call.
These functions, which start with "DL", are called dynamic load APIs and provide the following interfaces to support dynamic load operations:
void Const Char int
The function of Dlopen is to open the library file and return the file handle, the mode parameter indicates the timing of the relocation, there are rtld_now and rtld_lazy two values, this is what we mean after the analysis.
The function of the Dlsym function is to find the address of the function from the newly loaded library based on the name of the function passed in.
Char *dlerror ();
Dlerror does not have an entry, it returns the string of the last error that occurred
Char void *handle);
Dlclose Close the Open library file, in fact, if there are other programs currently referencing this library, it will not be closed, multiple programs are shared with a reference counting mechanism, when the last program to refer to it closed it will not really close. These functions need to include the <dlfcn.h> header file when they are used.
I still use this simple demo program before Sumapp to demonstrate the use of the dynamic loading library.
First of all, the libsum.so generation mode is the same, the specific can refer to the previous article. MAIN.C code We need to change, re-dlopen the way to refer to libsum.so, the code is as follows:
192: Zch kane$ lsdemodlopen.c main.c sum.h sumappdlibsum.so sum.c sum.o192: Zch kane$192: Zch kane$192: Zch kane$192: Zch kane$ more demodlopen.c #include<stdio.h>#include<dlfcn.h>intMainvoid){ intINUM1 =1; intINum2 =2; void* Phandle =NULL; int(*pfunc) (int,int) =NULL; Char*Pcerror; intIRet =0; Phandle= Dlopen ("libsum.so", Rtld_lazy); if(NULL = =phandle) {printf ("Open libsum.so failed!, error message is%s\r\n.", Dlerror ()); return 0; } PFunc= Dlsym (Phandle,"Sum"); if(NULL = =PFunc) {printf ("Find function Sum failed!, error message is%s\r\n.", Dlerror ()); return 0; } IRet= (*PFunc) (INum1, iNum2); printf ("IRet =%d\r\n", IRet); Dlclose (Phandle); return 0; }192: Zch kane$
Compile Demodlopen.c, execute SUMAPPD
192 gcc -rdynamic-o sumappd demodlopen.c-LDL192: Zch KANE$./SUMAPPD
IRet = 3
The compile-time-rdynamic is used to notify the linker to add all symbols to the dynamic symbol table (which is intended to be used dlopen
for backward tracking). -ldl
indicates that you must dllib
link to the program.
Summary: The dynamic link library at the time of loading is the process start, and the dynamic loading library is the program to determine the loading time. Look at the online there are some students do not understand exactly when to use the dynamic link library, when suitable for dynamic loading library. I said a scene I encountered at work is the dynamic loading. As we are doing Linux-based embedded software platform, need to support a variety of products, and some features or business, in a product may not support, in order to achieve the purpose of product differentiation. For example, business A may use libabc.so at run time, but libabc.so may be cut off on a product, so in this case, when compiling the version and writing the code, you have to dynamically load this way. Because if you use dynamic linking, and libabc.so is just being cut off on this product, it will cause the process of business A to start failing (dynamic linking fails to load the dynamic library when the process starts).
For more in-depth articles, please refer to http://www.ibm.com/developerworks/cn/linux/l-dynamic-libraries/index.html
Dynamic link library and static link library under Linux What the hell is that? (iii) Dynamic loading library