GCC link xxx: No such file or directory and run executable file error while loading shared libraries: xxx.so solution

About GCCprompt can not find the file specified library this problem usually occurs in the following two scenarios:

  • When using the static library, or dynamic link library file to achieve operation (executable file), * GCCyou may be prompted xxx:No such file or directory(which xxxrepresents the failure to find a static library or DLL);
  • With the implementation of dynamic libraries generated executable file, GCCyou may be prompted ./main.exe: error while loading shared libraries: xxx.so: cannot open shared object file: No such file or directory(which xxxrepresents the file name of the dynamic library).

1. The library file cannot be found when GCC generates an executable file

To completely solve this problem, readers must first understand GCCthe path where the compiler searches for library files by default when generating executable files .

There are two ways to specify the library file used in the program linking stage. Suppose the current mian.cppfile it needs libmyfunction.ato complete the link, the link to complete the operation gccinstruction has the following two kinds of writing:

wohu@ubuntu:~/cpp/src$ gcc -static main.c libmyfunction.a -o main.exe
wohu@ubuntu:~/cpp/src$ gcc -static main.c -lmyfunction -o main.exe

When the link to the first writing operation is completed, GCCthe compiler will only be in the current directory (this is the demo directory) to find libmyfunction.aa static link library; on the contrary, if a -l(lowercase L) option specifies the file name you want to find a static library, the GCCcompiler in the following order, in order to specify the directory to find the required library files:

  1. If the gccinstruction uses the -Loption to specify a search path, the GCCcompiler will give priority to library files needed to find under this path;
  2. Then Linuxthe system LIBRARY_PATHenvironment variables to specify the path to search for library files needed;
  3. Finally, the GCCcompiler default search path (such as /lib, /lib64, /usr/lib, /usr/lib64, /usr/local/lib, /usr/local/lib64and so on, different system environments differ slightly) looks.

If readers use the first method to complete the link operation, but GCCthe compiler not able to find the required library files, indicating that use library files are not stored in the current path, the solution is to manually locate the library file and move it to the current path, Then perform the link operation again.

On the contrary, if the reader is using the second method and encounters the GCC compiler prompt that the required library file is not found, it indicates that the storage path of the library file is incorrect. There are three solutions:

  • Manually locate the library file and gcccommand with -Loptions clearly indicate its storage path. For example, libmyfunction.astatic library files are stored in /usrthe directory, then complete the link operation gccinstructions should be
gcc -static main.c -L/usr -lmymath -o main.exe
  • Adding library files to the storage path LIBRARY_PATHenvironment variable. Library file is still stored in the /usrdirectory, by executing
export LIBRARY_PATH=$LIBRARY_PATH:/usr

Command to the /usrdirectory to the environment variable (in this embodiment is only valid in the current command line window);

  • The mobile library files to the GCCcompiler default search path.

2. The dynamic library file cannot be found when GCC is running the executable file

The implementation of the generated executable file, if GCCthe compiler prompted not find the required library files, which means that GCCthe compiler can not find some of dynamic libraries support the executable file to run.

In fact, when GCCthe compiler to run an executable file, the following sequence will follow the path of the search for the required DLL file:

  1. If when the executable file, the user uses -Wl, -rpath=dir(which dirrepresents a specific path to look for, if there are multiple paths to find the middle with :time, the colon) search path option specifies dynamic library, then run the file GCCwill be the first to designated Find the required library file in the path;
  2. GCCThe compiler will go to LD_LIBRARY_PATHthe path specified in the environment variable to find the dynamic library files needed;
  3. GCCThe compiler will go to /ect/ld.so.confthe file specified in the search path to find the dynamic library files;
  4. GCCThe compiler will go to the default search path (such as /lib, /lib64, /usr/lib, /usr/lib64etc.) to find the required DLL file.

Note that the current storage path of the executable file is not within the range of the default search path. Therefore, even if the dynamic library file and the executable file are placed in the same directory, the GCCcompiler may prompt "Cannot find the dynamic library".

Therefore, for the GCCquestion can not find the dynamic library files when running the executable file, common solution is:

  • The storage path dynamic library files added to the LD_LIBRARY_PATHenvironment variable. Assuming that the dynamic library files are stored in the /usrdirectory, informed the Executive
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr

Command to achieve this purpose (this method is only valid in the current command line window);

  • Modify the storage path dynamic library files, is about to move it to GCCthe compiler default search path;
  • Modify ~/.bashrcor ~/.bash_profilefile, the file is added in the last line
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:xxx

(It xxxis the absolute storage path of the dynamic library file). Once saved, the execution source bashrcof instructions (this way only the current logged in user valid).

It is worth mentioning that the GCCcompiler provides lddcommand, with the instruction, we can know exactly what dynamic libraries an executable file needs to be done to support, whether they have found a dynamic library files, specific storage path of each dynamic library files, etc. information.

In the previous example, for example, execute the following lddinstructions:

wohu@ubuntu:~/cpp/src$ ldd main.exe
linux-vdso.so.1 =>  (0x00007fff06fb3000)
libmymath.so => /lib64/libmymath.so (0x00007f65b2a62000)
libc.so.6 => /lib64/libc.so.6 (0x00000037e2c00000)
/lib64/ld-linux-x86-64.so.2 (0x00000037e2800000)

Note that if a DLL file is not found, =>the back will be displayed not found, indicating that GCCthe compiler can not find the dynamic library, then the executable file will not be executed.

Guess you like

Origin blog.csdn.net/wohu1104/article/details/110847090