As we all know, the default search paths for Linux dynamic libraries are /lib and /usr/lib. After the dynamic library is created, it is usually copied to these two directories. When a program needs to execute a dynamic library, and the dynamic library has not been loaded into the memory, the system will automatically find the corresponding dynamic library file in the two default search paths, and then load the file into the memory, so that the program You can use the functions in the dynamic library, as well as other resources of the dynamic library. In Linux, the search path of the dynamic library can be specified in the following three ways in addition to the default search path.
Method 1: Specify the dynamic library search path in the configuration file /etc/ld.so.conf.
You can specify the search path for the dynamic library by editing the configuration file /etc/ld.so.conf, which acts as a dynamic library search path. After editing the file, you must run the command ldconfig to make the modified configuration take effect. We illustrate this method by Example 1.
Example 1:
We use the following command to create the dynamic library libpos.so with the source program pos_conf.c (see program 1). For detailed creation process, please refer to [1].
# gcc -c pos_conf.c # gcc -shared -fPCI -o libpos.so pos_conf.o #
#include <stdio.h> void pos() { printf("/root/test/Conf/lib\ "); } Program 1: pos_conf.c
Then compile main.c (see program 2) with the following command to generate the target program pos.
# gcc -o pos main.c -L. -lpos #
void pos(); int main() { pos(); return 0; } Program 2: main.c
Then put the library The file is moved to the directory /root/test/conf/lib.
# mkdir -p /root/test/conf/lib # mv libpos.so /root/test/conf/lib #
Last edit the configuration file /etc/ld.so.conf and append it to this file One line "/root/test/conf/lib".
Run the program pos try.
# ./pos ./pos: error while loading shared libraries: libpos.so: cannot open shared object file: No such file or directory #
An error occurred, the system did not find the dynamic library libpos.so. Looking for the reason, after editing the configuration file /etc/ld.so.conf, the command ldconfig was not run, so the modification has not yet taken effect. Let's try it after running ldconfig.
# ldconfig # ./pos /root/test/conf/lib #
The program pos runs successfully and prints the correct result.
Method 2: Specify the dynamic library search path through the environment variable LD_LIBRARY_PATH.
The dynamic library search path can also be specified by setting the environment variable LD_LIBRARY_PATH. When multiple dynamic library search paths are specified through this environment variable, the paths are separated by a colon ":". The method will be described below by way of Example 2.
Example 2:
We use the following command to create the dynamic library libpos.so with the source program pos_env.c (see program 3).
# gcc -c pos_env.c # gcc -shared -fPCI -o libpos.so pos_env.o #
#include <stdio.h> void pos() { printf("/root/test/Env/lib\ "); } Program 3: pos_env.c
The executable file pos for testing can use the target program pos obtained in Example 1, and does not need to be compiled again. Because the function pos in pos_conf.c and the function pos function prototype in pos_env.c are the same, and the dynamic library name is the same, this is like re-creating the library after modifying the dynamic library pos. This is one of the advantages of using a dynamic library.
Then move the dynamic library libpos.so to the directory /root/test/conf/lib.
# mkdir -p /root/test/env/lib # mv libpos.so /root/test/env/lib #
We can use export to set the environment variable, after setting the environment variable This environment variable is valid in the command.
Example:
# export LD_LIBRARY_PATH=/root/test/env/lib #
But for the convenience of this article, another way to set environment variables is to add environment variables before the command. The environment variable is only valid for this command. When the command is executed, the environment variable is invalid. Such as the following command:
# LD_LIBRARY_PATH=/root/test/env/lib ./pos /root/test/env/lib #
Program pos runs successfully, and the result of printing is "/root/test/Env/lib", is the result of the function pos in the program pos_env.c. So the dynamic library searched by the program pos is /root/test/env/lib/libpos.so.
Nginx has been widely used in production environments compared to Apaches high performance and high
1. /The root partition is a mandatory partition, partitioned according to your actual situation. 2.
Azure is one of the representatives of more and more companies considering moving their business to
Bill · Gates attitude towards software is: If you want good software, you have to pay for it.
Linux basic command summary --vi editor
How to solve the problem that CentOS Linux can't mount the CD-ROM drive
Network Tutorial: Implementing Internet Filtering under Linux System
Three steps to teach you how to create a Linux flash drive
Linux Chinese dialog box garbled solution under Linux
How low the configuration can run Linux
Yum from the mount CD or ISO installation basic tutorial
Getting Started with AT&T Syntax under Linux (GNU as Assembly Syntax)
Talking about Win8 cloud connection: Microsoft cloud, everything is no longer a problem
Win7 open notepad appears garbled solution summary
Win7 system volume icon is missing how to recover?
Win8 interface mouse control corner and edge tips
How to calibrate the Win8.1 system screen?
What if Win7 cannot modify the desktop status?
Win7 open PDF file will prompt the number out of the border how to do?
Android is difficult to change Windows8 into PC counterattack opportunities