Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208837
  • 博文数量: 78
  • 博客积分: 3169
  • 博客等级: 中校
  • 技术积分: 805
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-25 13:50
文章分类
文章存档

2012年(1)

2011年(77)

分类: LINUX

2011-04-06 18:37:15

Linux操作系统上面的动态共享库大致分为三类:

1、操作系统级别的共享库和基础的系统工具库

比方说libc.so, libz.so, libpthread.so等等,这些系统库会被放在/lib和/usr/lib目录下面,如果是64位操作系统,还会有/lib64和/usr /lib64目录。如果操作系统带有图形界面,那么还会有/usr/X11R6/lib目录,如果是64位操作系统,还有/usr/X11R6 /lib64目录。此外还可能有其他特定Linux版本的系统库目录。这些系统库文件的完整和版本的正确,确保了Linux上面各种程序能够正常的运行。

2、应用程序级别的系统共享库

并非操作系统自带,但是可能被很多应用程序所共享的库,一般会被放在/usr/local/lib和/usr/local/lib64这两个目录下面。很多你自行编译安装的程序都会在编译的时候自动把/usr/local/lib加入gcc的-L参数,而在运行的时候自动到/usr/local/lib下面去寻

找共享库。

以上两类的动态共享库,应用程序会自动寻找到他们,并不需要你额外的设置和担心。这是为什么呢?因为以上这些目录默认就被加入到动态链接程序的搜索路径里面了。Linux的系统共享库搜索路径定义在/etc/ld.so.conf这个配置文件里面。这个文件的内容格式

大致如下:

1. /usr/X11R6/lib64

2. /usr/X11R6/lib

3. /usr/local/lib

4. /lib64

5. /lib

6. /usr/lib64

7. /usr/lib

8. /usr/local/lib64

9. /usr/local/ImageMagick/lib

假设我们自己编译安装的ImageMagick图形库在/usr/local/ImageMagick目录下面,并且希望其他应用程序都可以使用ImageMagick的动态共享库,那么我们只需要把/usr/local/ImageMagick/lib目录加入/etc /ld.so.conf文件里面,然后执行:ldconfig 命令即可。

ldcofig将搜索以上所有的目录,为共享库建立一个缓存文件/etc/ld.so.cache。为了确认ldconfig已经搜索到ImageMagick的库,我们可以用上面介绍的strings命令从ld.so.cache里面抽取文本信息来检查一下:

1. strings /etc/ld.so.cache | grep ImageMagick

输出结果为:

1. /usr/local/ImageMagick/lib/libWand.so.10

2. /usr/local/ImageMagick/lib/libWand.so

3. /usr/local/ImageMagick/lib/libMagick.so.10

4. /usr/local/ImageMagick/lib/libMagick.so

5. /usr/local/ImageMagick/lib/libMagick++.so.10

6. /usr/local/ImageMagick/lib/libMagick++.so

已经成功了!

3、应用程序独享的动态共享库

有很多共享库只被特定的应用程序使用,那么就没有必要加入系统库路径,以免应用程序的共享库之间发生版本冲突。因此Linux还可以通过设置环境变量 LD_LIBRARY_PATH来临时指定应用程序的共享库搜索路径,就像我们上面举的那个例子一样,我们可以在应用程序的启动脚本里面预先设置 LD_LIBRARY_PATH,指定本应用程序附加的共享库搜索路径,从而让应用程序找到它。

编译的时候动态库的搜索路径:

1、gcc会去找-L

2、环境变量LIBRARY_PATH

3、 /lib /usr/lib /usr/local/lib 这俩是当初compile gcc时写在程序内的

运行时动态库的搜索路径搜索的先后顺序是:

1.编译目标代码时指定的动态库搜索路径; 通过gcc 的参数"-Wl,-rpath,"指定。当指定多个动态库搜索路径时,路径之间用冒号":"分隔)

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

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

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

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




环境变量PATH,保存了一系列目录,它在系统中的主要作用是,提供命令的搜索路径

第一种方法:
在用户主目录下有一个 .bashrc 文件,可以在此文件中加入 PATH 的设置如下:
export PATH=”$PATH:/your path1/:/your path2/…..”
注意:每一个 path 之间要用 “:“ 分隔。

第二种方法:
在 /etc/profile中增加。

# vim /etc/profile
在文档最后,添加:
export PATH="/opt/STM/STLinux-2.3/devkit/sh4/bin:$PATH"
保存,退出,然后运行:
# source /etc/profile

第三种:在/etc/ld.so.conf中添加 etc /ld.so.conf文件里面,然后执行:ldconfig 命令即可。

第四种:[root@localhost u-boot-sh4]# export PATH=/opt/STM/STLinux-2.3/devkit/sh4/bin:$PATH

第五种通过设定环境变量 LD_LIBRARY_PATH 也可以指定动态库搜索路径。当通过该环境变量指定多个动态库搜索路径时,路径之间用冒号 " " 分隔。

我们可以使用 export 来设置该环境变量,在设置该环境变量后所有的命令中,该环境变量都有效。

例如:

# export LD_LIBRARY_PATH=/root/test/env/lib
         #

但本文为了举例方便,使用另一种设置环境变量的方法,既在命令前加环境变量设置,该环境变量只对该命令有效,当该命令执行完成后,该环境变量就无效了。如下述命令:

# LD_LIBRARY_PATH=/root/test/env/lib ./pos
        /root/test/env/lib
         #

如果要使得计时生效,使用 source 命令 source .bashrc,修改当前shell环境变量

PATH的值是一系列目录,当您运行一个程序时,Linux在这些目录下进行搜寻编译链接。

可用 export 命令查看PATH值

[root@localhost u-boot-sh4]# export
declare -x CVS_RSH="ssh"
declare -x DISPLAY=":0.0"
declare -x G_BROKEN_FILENAMES="1"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x HOSTNAME="localhost"
declare -x INPUTRC="/etc/inputrc"
declare -x LANG="zh_CN.UTF-8"
declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="root"
declare -x LS_COLORS="no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:"
declare -x MAIL="/var/spool/mail/root"
declare -x OLDPWD="/root"
declare -x PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
declare -x PWD="/opt/STM/STLinux-2.3/devkit/sources/u-boot/u-boot-sh4"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_ASKPASS="/usr/libexec/openssh/gnome-ssh-askpass"
declare -x TERM="xterm"
declare -x USER="root"
declare -x XAUTHORITY="/root/.xauthkSzH7b"

单独查看PATH环境变量,可用:

[root@localhost u-boot-sh4]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

添加PATH环境变量,可用:

[root@localhost u-boot-sh4]# export PATH=/opt/STM/STLinux-2.3/devkit/sh4/bin:$PATH

再次查看:

[root@localhost u-boot-sh4]# echo $PATH
/opt/STM/STLinux-2.3/devkit/sh4/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

说明添加PATH成功。

/sbin /usr/sbin目录下的命令为系统命令没有加入普通用户的PATH中所以,普通用户无法使用其中的命令。

Search Path

GCC looks in several different places for headers. On a normal Unix system, if you do not instruct it otherwise, it will look for headers requested with #include <file> in:

/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include

For C programs, it will also look in /usr/include/gv3, first. In the above, target is the canonical name of the system GCC was configured to compile code for; often but not always the same as the canonical name of the system it runs on. version is the version of GCC in use.

You can add to this list with the -Idir command line option. All the directories named by -I are searched, in left-to-right order, before the default directories. The only exception is when dir is already searched by default. In this case, the option is ignored and the search order for system directories remains unchanged.

Duplicate directories are removed from the quote and bracket search chains before the two chains are merged to make the final search chain. Thus, it is possible for a directory to occur twice in the final search chain if it was specified in both the quote and bracket chains.

You can prevent GCC from searching any of the default directories with the -nostdinc option. This is useful when you are compiling an operating system kernel or some other program that does not use the standard C library facilities, or the standard C library itself. -I options are not ignored as described above when -nostdinc is in effect.

GCC looks for headers requested with #include "file" first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets"<>的意思". For example, if /usr/include/sys/stat.h contains #include "types.h", GCC looks for types.h first in /usr/include/sys, then in its usual search path.

`#line' (see ) does not change GCC's idea of the directory containing the current file.

You may put -I- at any point in your list of -I options. This has two effects. First, directories appearing before the -I- in the list are searched only for headers requested with quote marks(“”的意思). Directories after -I- are searched for all headers. Second, the directory containing the current file is not searched for anything, unless it happens to be one of the directories named by an -I switch. -I- is deprecated, -iquote should be used instead.

-I. -I- is not the same as no -I options at all, and does not cause the same behavior for `<>' includes that `""' includes get wi

阅读(1146) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~