Let a compiled ELF executable program look for the dynamic library .so file needed at runtime in the current directory

Use the LD_LIBRARY_PATH environment variable to specify the dynamic library search path. The specific steps are as follows:
Copy the executable program and the dynamic library file to the same directory, such as /path/to/myapp.

Open the terminal and enter the /path/to/myapp directory.

Set the LD_LIBRARY_PATH environment variable to add the current directory to the dynamic library search path, for example:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

Here .represents the current directory, $LD_LIBRARY_PATHrepresenting the original dynamic library search path. Use exportthe command to LD_LIBRARY_PATHexport the environment variable to the environment of the current shell, so that subsequent commands can use the environment variable.

Run executable programs such as:

./myapp

At runtime, the dynamic library linker will first search for the dynamic library file in the current directory, and if it cannot find it, it will continue to search in other paths.

Note that using the LD_LIBRARY_PATH environment variable to specify the dynamic library search path may be a security risk, because it allows users to load dynamic library files from arbitrary locations at runtime. Therefore, it is recommended to use this method only in the development and testing phase, and not in production environment.

Guess you like

Origin blog.csdn.net/weixin_43074760/article/details/131470652