In traditional mode, the library function link is completed in the compiler, and all related objects are integrated into an executable file during compilation. In comparison, we can also set
In traditional mode, the library function link is completed in the compiler, and all related objects are integrated into an executable file during compilation. In comparison, we can also postpone loading the library function link to the running period, that is, what we call dynamic link.
Advantages of dynamic links
In addition to all the modularity and code reuse of the static link Library, the dynamic link library also has the following advantages.
You can share databases between processes:
When multiple processes share a single database (for example, the stl library and some system libraries are basically used by most programs), the dynamic link method can only retain one copy in the memory to save memory.
Upgrade becomes simple:
You only need to upgrade the dynamic link library without re-compiling the link to other original code to complete the upgrade of the entire program (many Windows patches are released in this way ).
It can be dynamically loaded:
When the software is relatively large, you can dynamically load/uninstall the corresponding link library as needed, instead of loading all the links at a time as static links do.
Create a dynamic link library
The method for creating a dynamic link library is relatively simple. in the previous example of a static link library, we only need to use the gcc-shared command to create a libstack. so dynamic library (static library generally uses. a is used as the extension, and the dynamic library generally uses. so as the extension ).
Gcc-shared-o libstack. so stack. o
The method of using dynamic databases in the link phase is basically the same as that of static databases.
Gcc-o run main. c-L.-lstack
After compiling and playing this program, we found that it reported an error message that could not be found in the dynamic link library.
Tianfang>./run
./Run: error while loading shared libraries: libstack. so: cannot open shared object file: No such file or directory
You can also run the ldd command to view the dependency of a program on the dynamic link library:
Tianfan> ldd run
Linux-gate.so.1 (0xb7707000)
Libstack. so => not found
Libc. so.6 =>/lib/libc. so.6 (0xb7546000)
/Lib/the ld-linux.so.2 (0xb7708000)
The result of ldd indicates that the generated libstack. so cannot be found. Because the dynamic link library is a file that can be shared, it is often stored in a public location. in Linux, the rules for programs to find the dynamic link library are as follows:
First, search for the path recorded by the environment variable LD_LIBRARY_PATH.
Search for the cached file/etc/ld. so. cache.
If none of the above steps can be found, go to the default system path, first/lib and then/usr/lib.
Obviously, these paths do not contain the current path. To solve the above problem, a simple method is to add the current path to the environment variable:
Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH :.
Then, use the ldd name again to test and find our linked library.
Tianfan> ldd run
Linux-gate.so.1 (0xb77ce000)
Libstack. so =>./libstack. so (0xb77ca000)
Libc. so.6 =>/lib/libc. so.6 (0xb760a000)
/Lib/ld-linux.so.2 (0xb77cf000)
However, it is not recommended that you modify LD_LIBRARY_PATH.
LD_LIBRARY_PATH is not the answer http://prefetch.net/articles/linkers.badldlibrary.html
Why LD_LIBRARY_PATH is bad http://xahlee.org/UnixResource_dir/_/ldpath.html
LD_LIBRARY_PATH-just say no http://blogs.sun.com/rie/date/20040710
Summary
This article describes how to use the gcc command to create a dynamic link library, how to use the ldd command to view the dependency on the dynamic link library, and how to solve the problem that the dynamic link library cannot be found. I have not provided a detailed introduction to the principles of the dynamic link library. if you are interested, you can find related articles on the Internet.