About GCC
prompt 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), *
GCC
you may be promptedxxx:No such file or directory
(whichxxx
represents the failure to find a static library or DLL); - With the implementation of dynamic libraries generated executable file,
GCC
you may be prompted./main.exe: error while loading shared libraries: xxx.so: cannot open shared object file: No such file or directory
(whichxxx
represents 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 GCC
the 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.cpp
file it needs libmyfunction.a
to complete the link, the link to complete the operation gcc
instruction 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, GCC
the compiler will only be in the current directory (this is the demo directory) to find libmyfunction.a
a static link library; on the contrary, if a -l
(lowercase L
) option specifies the file name you want to find a static library, the GCC
compiler in the following order, in order to specify the directory to find the required library files:
- If the
gcc
instruction uses the-L
option to specify a search path, theGCC
compiler will give priority to library files needed to find under this path; - Then
Linux
the systemLIBRARY_PATH
environment variables to specify the path to search for library files needed; - Finally, the
GCC
compiler default search path (such as/lib
,/lib64
,/usr/lib
,/usr/lib64
,/usr/local/lib
,/usr/local/lib64
and so on, different system environments differ slightly) looks.
If readers use the first method to complete the link operation, but GCC
the 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
gcc
command with-L
options clearly indicate its storage path. For example,libmyfunction.a
static library files are stored in/usr
the directory, then complete the link operationgcc
instructions should be
gcc -static main.c -L/usr -lmymath -o main.exe
- Adding library files to the storage path
LIBRARY_PATH
environment variable. Library file is still stored in the/usr
directory, by executing
export LIBRARY_PATH=$LIBRARY_PATH:/usr
Command to the /usr
directory to the environment variable (in this embodiment is only valid in the current command line window);
- The mobile library files to the
GCC
compiler 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 GCC
the compiler prompted not find the required library files, which means that GCC
the compiler can not find some of dynamic libraries support the executable file to run.
In fact, when GCC
the compiler to run an executable file, the following sequence will follow the path of the search for the required DLL file:
- If when the executable file, the user uses
-Wl
,-rpath=dir
(whichdir
represents 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 fileGCC
will be the first to designated Find the required library file in the path; GCC
The compiler will go toLD_LIBRARY_PATH
the path specified in the environment variable to find the dynamic library files needed;GCC
The compiler will go to/ect/ld.so.conf
the file specified in the search path to find the dynamic library files;GCC
The compiler will go to the default search path (such as/lib
,/lib64
,/usr/lib
,/usr/lib64
etc.) 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
GCC
compiler may prompt "Cannot find the dynamic library".
Therefore, for the GCC
question 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_PATH
environment variable. Assuming that the dynamic library files are stored in the/usr
directory, 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
GCC
the compiler default search path; - Modify
~/.bashrc
or~/.bash_profile
file, the file is added in the last line
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:xxx
(It xxx
is the absolute storage path of the dynamic library file). Once saved, the execution source bashrc
of instructions (this way only the current logged in user valid).
It is worth mentioning that the GCC
compiler provides ldd
command, 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 ldd
instructions:
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 GCC
the compiler can not find the dynamic library, then the executable file will not be executed.