3.2 prepare the environment for creating and using a dynamic library 3.2.1Create root folderdynamicDemo
:
[root@VM_120_243_centos ~]# mkdir dynamicDemo
Create a subfolderinclude
To store all header files:
[root@VM_120_243_centos ~]# cd staticDemo/[root@VM_120_243_centos dynamicDemo]# mkdir include
Go to the include folder and writemyLib.h
File, statementprintInfo()
Function:
[root@VM_120_243_centos dynamicDemo]# cd include/[root@VM_120_243_centos include]# vim myLib.h[root@VM_120_243_centos include]# cat myLib.h void printInfo();
Go to the upper-level directory and createlib
Folder, used to store all the library files, and then writeprint.c
File, usedmyLib.h
InprintInfo()
Function:
[root@VM_120_243_centos include]# cd ..[root@VM_120_243_centos dynamicDemo]# mkdir lib[root@VM_120_243_centos dynamicDemo]# cd lib/[root@VM_120_243_centos lib]# vim print.c[root@VM_120_243_centos lib]# cat print.c #include <stdio.h>#include "myLib.h"void printInfo(){ printf("print from print.c file...\n");}
Go to the upper-level directory and writemain.c
File for testing:
[root@VM_120_243_centos lib]# cd ..[root@VM_120_243_centos dynamicDemo]# vim main.c[root@VM_120_243_centos dynamicDemo]# cat main.c #include <stdio.h>#include "myLib.h"int main(void){ printf("print from main.c file...\n"); printInfo(); return 0;}
This completes the preparation. The entire directory structure is as follows:
[root@VM_120_243_centos dynamicDemo]# tree.├── include│ └── myLib.h├── lib│ └── print.c└── main.c2 directories, 3 files
3.2.2 generate a dynamic libraryEnter the lib folder and add it to gcc.-FPIC Parameters
To generate the print. o file:
[root@VM_120_243_centos dynamicDemo]# cd lib/[root@VM_120_243_centos lib]# gcc -c print.c -fPIC -I../include/ -o print.o[root@VM_120_243_centos lib]# lsprint.c print.o
Gcc-Shared Parameters
Generate dynamic library libprint. so:
[root@VM_120_243_centos lib]# gcc -shared print.o -o libprint.so[root@VM_120_243_centos lib]# lslibprint.so print.c print.o
3.2.3 generate executable filesEnterdynamicDemo
Directory, link to the database, and generate executable files:
[root@VM_120_243_centos dynamicDemo]# gcc main.c -I./include/ -L./lib/ -lprint -o main
Note: If the same directory contains both dynamic and static libraries with the same name, for examplelibprint.so
Andlibprint.a
All are in the current path, then gcc willGive priority to dynamic libraries.
Execute the executable file:
[root@VM_120_243_centos dynamicDemo]# ./main ./main: error while loading shared libraries: libprint.so: cannot open shared object file: No such file or directory
System error not foundlibprint.so
In Linux/etc/ld.so.cache
File Search for the dynamic library to be linked. While/etc/ld.so.cache
Yesldconfig
Program read/etc/ld.so.conf
File generation. (Note,/etc/ld.so.conf
Does not need to include/lib
And/usr/lib
,ldconfig
The program will automatically search for these two directories)
Here, we specify the environment variables to find the dynamic library path, which will be detailed in the next section:
[root@VM_120_243_centos dynamicDemo]# LD_LIBRARY_PATH=./lib/ ./mainprint from main.c file...print from print.c file...
3.3 set the search path for the dynamic library 3.3.1 LD_LIBRARY_PATHLD_LIBRARY_PATH
Is the Linux environment variable name, which is mainly usedSearch for paths other than the default path when searching for a dynamic library.
Generally, the default path for Linux to search for dynamic libraries is/lib
And/usr/lib
.LD_LIBRARY_PATH
Path priorityGreater than the system default path.
[root@VM_120_243_centos dynamicDemo]# gcc main.c -I./include/ -L./lib/ -lprint -o main[root@VM_120_243_centos dynamicDemo]# LD_LIBRARY_PATH=./lib/ ./mainprint from main.c file...print from print.c file...
3.3.2 rpathGcc-R
Or-rpath
Specifies the dynamic library path when compiling the link, and saves the dynamic library path to the executable file.
When running an executable file, you can directly search for the dynamic library in this path without relying on the default path or environment variable.
[root@VM_120_243_centos dynamicDemo]# gcc main.c -I./include/ -L./lib/ -lprint -Wl,-rpath=./lib -o main[root@VM_120_243_centos dynamicDemo]# ./main print from main.c file...print from print.c file...
In this way, each program can set an independent dynamic library location, insteadLD_LIBRARY_PATH
Environment variables affect other programs.
3.3.3 ldconfigldconfig
Is a dynamic link library management command. This command is used in the default path (/lib
And/usr/lib
) And dynamic library configuration files/etc/ld.so.conf
In the directory, search for the dynamic library, and thenDynamic Loading Program (ld. so)
Create a dynamic library listCache file (/etc/ld. so. cache)
.
3.3.3.1 add standard pathPut the/usr. lib folder as an example:
[root@VM_120_243_centos dynamicDemo]# cp ./lib/libprint.so /usr/lib/
Update cache:
[root@VM_120_243_centos dynamicDemo]# ldconfig
View the dynamic library:
[root@VM_120_243_centos dynamicDemo]# ldconfig -p | grep libprint.so libprint.so (libc6,x86-64) => /lib/libprint.so
Compile and execute:
[root@VM_120_243_centos dynamicDemo]# gcc main.c -I ./include/ -L ./lib/ -lprint -o main[root@VM_120_243_centos dynamicDemo]# ./main print from main.c file...print from print.c file...
3.3.3.2 Add a configuration fileFirst View/etc/ld.so.conf
File:
[root@VM_120_243_centos dynamicDemo]# cat /etc/ld.so.confinclude ld.so.conf.d/*.conf
You can see that it containsld.so.conf.d
Directory, so the custom configuration filelibprint.conf
Add/etc/ld.so.conf.d
Folder, the configuration file contains the dynamic library path of the program:
[root@VM_120_243_centos dynamicDemo]# pwd/root/dynamicDemo[root@VM_120_243_centos dynamicDemo]# vim /etc/ld.so.conf.d/libprint.conf[root@VM_120_243_centos dynamicDemo]# cat /etc/ld.so.conf.d/libprint.conf/root/dynamicDemo/lib
Update cache:
[root@VM_120_243_centos dynamicDemo]# ldconfig
View the dynamic library:
[root@VM_120_243_centos dynamicDemo]# ldconfig -p | grep libprint.so libprint.so (libc6,x86-64) => /root/dynamicDemo/lib/libprint.so
Compile and execute:
[root@VM_120_243_centos dynamicDemo]# gcc main.c -I ./include/ -L ./lib/ -lprint -o main[root@VM_120_243_centos dynamicDemo]# ./main print from main.c file...print from print.c file...
References[1] JollyWing. Linux static library generation Guide [EB/OL]. http://www.cnblogs.com/jiqingwu/p/4325382.html.
[2] JollyWing. Linux dynamic library generation and use guide [EB/OL]. http://www.cnblogs.com/jiqingwu/p/linux_dynamic_lib_create.html.