| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > 软件开发

Linux操作系统的头文件和库文件搜索路径

时间:2008-02-20 10:11:09  来源:Linux联盟收集整理  作者:
 

Include的header文件,连结数据库,系统定义,总共有下列来源指定gcc去那找。 GvxLinux联盟

当初在编译时指定的(在~gcc/gcc/collect2.c:locatelib() GvxLinux联盟

写在specs内的 GvxLinux联盟

后来用-D -I -L指定的 GvxLinux联盟

gcc环境变量设定(编译的时候) GvxLinux联盟

ld.so的环境变量(这是run time的时候) GvxLinux联盟

一、头文件 GvxLinux联盟

gcc 在编译时如何去寻找所需要的头文件 : GvxLinux联盟

※所以header file的搜寻会从-I开始 GvxLinux联盟

※然后找gcc的环境变量 C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_PATH GvxLinux联盟

※再找内定目录 GvxLinux联盟

/usr/include GvxLinux联盟

/usr/local/include GvxLinux联盟

/usr/lib/gcc-lib/i386-linux/2.95.2/include GvxLinux联盟

/usr/lib/gcc-lib/i386-linux/2.95.2/../../../../include/g++-3 GvxLinux联盟

/usr/lib/gcc-lib/i386-linux/2.95.2/../../../../i386-linux/include GvxLinux联盟

库文件但是如果装gcc的时候,是有给定的prefix的话,那么就是 GvxLinux联盟

/usr/include GvxLinux联盟

prefix/include GvxLinux联盟

prefix/xxx-xxx-xxx-gnulibc/include GvxLinux联盟

prefix/lib/gcc-lib/xxxx-xxx-xxx-gnulibc/2.8.1/include GvxLinux联盟

二、库文件 GvxLinux联盟

cos()等函式库的选项要多加 -lm GvxLinux联盟

编译的时候: GvxLinux联盟

※gcc会去找-L GvxLinux联盟

※再找gcc的环境变量LIBRARY_PATH GvxLinux联盟

※再找内定目录 /lib /usr/lib /usr/local/lib 这是当初compile gcc时写在程序内的 GvxLinux联盟

三、运行时动态库的搜索路径 GvxLinux联盟

1、在配置文件/etc/ld.so.conf中指定动态库搜索路径 GvxLinux联盟

2、通过环境变量LD_LIBRARY_PATH指定动态库搜索路径(当通过该环境变量指定多个动态库搜索路径时,路径之间用冒号":"分隔) GvxLinux联盟

3、在编译目标代码时指定该程序的动态库搜索路径(还可以在编译目标代码时指定程序的动态库搜索路径。 GvxLinux联盟

这是通过gcc 的参数"-Wl,-rpath,"指定(如例3所示)。当指定多个动态库搜索路径时,路径之间用冒号":"分隔) GvxLinux联盟

4、默认的动态库搜索路径/lib GvxLinux联盟

5、默认的动态库搜索路径/usr/lib GvxLinux联盟

可以通过执行可执行文件pos得到的结果不同获知其搜索到了哪个动态库,从而获得第1个动态库搜索顺序,然后删除该动态库, GvxLinux联盟

再执行程序pos,获得第2个动态库搜索路径,再删除第2个被搜索到的动态库, GvxLinux联盟

如此往复,将可得到Linux搜索动态库的先后顺序。 GvxLinux联盟

程序pos执行的输出结果和搜索到的动态库的对应关系如表1所示 GvxLinux联盟

程序pos输出结果 使用的动态库 对应的动态库搜索路径指定方式 GvxLinux联盟

./ ./libpos.so 编译目标代码时指定的动态库搜索路径 GvxLinux联盟

/root/test/env/lib /root/test/env/lib/libpos.so 环境变量LD_LIBRARY_PATH指定的动态库搜索路径 GvxLinux联盟

/root/test/conf/lib /root/test/conf/lib/libpos.so 配置文件/etc/ld.so.conf中指定的动态库搜索路径 GvxLinux联盟

/lib /lib/libpos.so 默认的动态库搜索路径/lib GvxLinux联盟

/usr/lib /usr/lib/libpos.so 默认的动态库搜索路径/usr/lib GvxLinux联盟

综合以上结果可知,动态库的搜索路径搜索的先后顺序是: GvxLinux联盟

1.编译目标代码时指定的动态库搜索路径; GvxLinux联盟

2.环境变量LD_LIBRARY_PATH指定的动态库搜索路径; GvxLinux联盟

3.配置文件/etc/ld.so.conf中指定的动态库搜索路径; GvxLinux联盟

4.默认的动态库搜索路径/lib; GvxLinux联盟

5.默认的动态库搜索路径/usr/lib。 GvxLinux联盟

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门