In the previous article, I probably described the compilation and use of static link libraries under Linux, and the methods of compiling and using dynamic link libraries.
1. What is a dynamic link library
The so-called dynamic link library, refers to the compilation of the program will not be referenced to the library inserted into the execution of the program, but at the time of execution to load the relevant library, all the program used in this library can share a piece of code.
The benefit is that the executable program takes up less space, and if the library needs to be upgraded, you don't need to recompile your program, just upgrade the relevant library.
2. How to compile and use dynamic link library
Use the same code as the previous article: Main.c sum.c sum.h
Dynamic link library files under Linux This is usually the name: libxxx.so
copy code [email protected]: $ lsmain.c sum.c sum.h[email protected]: $ [email protected]: $ gcc-c-fpic sum.c ##
-FIPC tells the compiler to compile the source code into a shared object file, and the PIC (position-independent code) means the non-positional dependency code [email protected]: $ lsmain.c sum.c sum.h Sum.o[email protected]: $ gcc-shared-fpic-o libsum.so sum.o # # generate a dynamic link library file Libsum.so[email Protected]: $ lslibsum.so main.c sum.c sum.h sum.o[email protected]: $ [email protected]: $ gcc-o SUMAPPD main.c-l. -lsum # # Generate executable program [email protected]: $ lslibsum.so sum.c sum.omain.c sum.h sumappd[ Email protected]: $./SUMAPPD # # Run Build results Num1 + Num2 = 3
Conclusion: The method of "-lsum" at compile time is not able to distinguish whether static or dynamic link is present.
If you have both a static-link library and a dynamic-link library in the same directory, the system will refer to the dynamic-link library by default, and if you want to use a static-link library, you will need to add the "-static" parameter at compile time.
(Note: If you write in C + +, you can use GCC instead of g++ when compiling)
Dynamic link library and static link library under Linux What the hell is that? (ii) Compilation and use of dynamic link libraries