a.根据环境变量LD_LIBRARY_PATH查找
b.根据/etc/ld.so.cache查找
c.查找依次在/lib和/usr/lib目录查找。
a 举例:
由于动态库的链接和运行时的目录是分开指定的,所以编译通过后,运行时boost库找不到的问题存在于当前工程中。解决办法就是设置外环境变量LD_LIBRARY_PATH:
#export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
这样就可以把boost库的所在目录/usr/local/lib加入到环境变量中去。
b举例:
1> 在sudo vim /etc/ld.so.conf 中加入 指定的路径 , eg, /usr/local/lib
直接加入一行 /usr/local/lib
2># sudo ldconfig
做完上述两步后,程序运行时就会在 /usr/local/lib中去查找动态库
Tip:
unbuntu中,sudo命令执行时,运行程序是不会去找环境变量LD_LIBRARY_PATH的,但是会找b和c。
Linux上依赖的动态库一般都在/usr/lib下;
二、gcc编译时头文件搜索顺序
#include <> : 直接到系统指定的某些目录中去找某些头文件。
#include “” : 先到源文件所在文件夹去找,然后再到系统指定的某些目录中去找某些头文件。
1、搜寻会从-I指定的目录开始搜索
2、然后找gcc的环境变量C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_PATH,根据环境变量搜索
3、再根据gcc默认的目录查找,如
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.1.2/include
/usr/include
三、命令查看gcc查找include文件的默认目录
1> 如果是c程序,则用命令 echo | gcc -v -x c -E -
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --with-tune=i686 --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
/usr/lib/gcc/i486-linux-gnu/4.1.2/cc1 -E -quiet -v - -mtune=i686
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.1.2/include
/usr/include
End of search list.
2> 如果是c++程序,则用命令 echo | gcc -v -x c++ -E -
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --with-tune=i686 --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
/usr/lib/gcc/i486-linux-gnu/4.1.2/cc1plus -E -quiet -v -D_GNU_SOURCE - -mtune=i686
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/i486-linux-gnu
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.1.2/include
/usr/include
End of search list.
参考: