In recent years, as I have participated in the construction of the relevant professional software Linux operating environment for many times, I feel it is necessary to take this knowledge into consideration for future reference.
Compile time and run time
Throughout the process of program compilation, the segments can be divided into compilation (Compiling refers to the layer from the language to the target file on the platform) and link (Linking, refers to the final formation of executable files from the target file), this general process can be called compile time; for Dynamic Links, there is also a runtime, that is, when a program is loaded by the operating system, the system loads the dynamic library required by the program into the memory to start running the program. Clarifying the status of these two processes in general linux development, as well as understanding the files and running tools required for each "time" is very important for understanding linux dynamic links.
Linux shared library naming rules
Review the linux naming rules for dynamic libraries. Generally, they are named libname. so. x. y. z. The meanings and compatibility standards of each version number are shown in the following table. These are also the design specifications that programmers should try to comply with when designing the library program.
Version Number |
Meaning |
X |
Major version number, which indicates a major upgrade. Libraries of different major versions are incompatible. |
Y |
Minor version number, indicating incremental upgrade. Later versions are backward compatible with libraries with minor version numbers. |
Z |
Release version number, indicating some error fixes, performance improvements, and full compatibility between different release versions |
This design can easily lead to a problem: how to effectively upgrade the dynamic library without requiring re-compilation of applications that depend on it? For example, demo. out depends on libsmath. so.1.0.0 (the name is specified in the compilation and stored in the DT_NEEDED segment of the ELF File). Now, the Library is upgraded to 1.1.0, but demo. the out program cannot use the new library. To solve this problem, a new mechanism SO-NANE is introduced in Linux.
SO-NAME Version Control
It can be seen from the above that the primary and secondary versions of the shared library determine the interface of the shared library. For applications that depend on a database, you only need to record the primary version number of the database to the minimum extent, the specific version is the next version and release version, can be handed over to the dynamic linker (generally for the ld-linux.so) to choose, so that you can solve the problem to a certain extent.
For the specific design implementation. so. x. y. z, omitting its y and z, only record as libname. so. x is the SO-NAME of the shared library. Record this SO-NAME in the DT_NEEDED segment of the application that requires the library, at the same time, add the soft link of the library in the library search directory of the dynamic linker, and specify it as libname. so. x (you can set/etc/ld. so. conf or set the environment variable LD_LIBRARY_PATH ). In this way, you only need to update the soft link to upgrade libname. so. x. y. z, and the application does not need to be re-compiled.
At the same time, SO-NAME will also be recorded in the dynamic library for the ldconfig tool to automatically update soft links, which is the reason why the tool runs after each installation of the new software package.
All of the above are for runtime, and some information about this library is also required during compilation, specifying the required library, that is, the gcc-lName. This option is primarily passed to the linker (ld, rather than the ld-linux.so used at runtime), which searches for libname in the library search directory (you can search by setting the-rpath parameter or LIBRARY_PATH environment variable. so file, so in order to use this library for development, you need to set the path to libname in the above directory. so. x or libname. so. x. y. the soft link of z, named libname. so. this work usually needs to be done manually.
Example
(In the following example, smath. h and libsmath. so are required for compilation; libsmath. so.1 and libsmath. so.1.0.0 are required for runtime)
Assume that you need to customize a set of mathematical libraries, including Add and Sub functions.
1 //smath.h2 3 int Add(int,int);4 5 int Sub(int,int);
1 //smath.c 2 3 int Add(int a,int b) 4 5 { 6 7 return a+b; 8 9 }10 11 int Sub(int a,int b)12 13 {14 15 return a-b;16 17 }
① Compile
Follow the gcc Operation Manual
Run
gcc -shared -fPIC -Wl, -soname,libsmath.so.1 -o libmath.so.1.0.0 smath.c
(If-soname is not specified, the Library does not have SO-NAME. The record in DT_NEEDED is blank)
② Installation
Copy header file
cp smath.h ~/include
Install it to the custom directory (non-/lib,/usr/lib, etc ~ /Lib
cp libsmath.so.1.0.0 ~/libcd ~/libln -s libsmath.so.1.0.0 libsmath.so.1ln -s libsmath.so.1 libsmath.so
③ Use
Make sure that smath. h and libsmath. so are in the relevant search directory (used for compiling links)
export C_INCLUDE_PATH=$HOME/includeexport LIBRARY_PATH=$HOME/include
(You can also set gcc parameters)
Now you can use gcc to compile the following code:
main.c#include <stdio.h>#include <smath.h>int main(){ int a=3,b=5; printf(“%d + %d = %d\n”,a,b,Add(a,b)); return 0;}
Compile
gcc -omain.out -lsmath main.c
④ Run
Ensure that libsmath. so.1 is in the relevant search directory (for running the link)
export LD_LIBRARY_PATH=$HOME/lib
(You can also set the/etc/ld. so. conf directory)
To run
Reference
Programmer self-cultivation-links, loads and libraries chapter 8th
Working with libraries and the linker -- http://bottomupcs.sourceforge.net/csbu/x4012.htm