现在的位置: 首页 > 综合 > 正文

编译时系统的环境变量

2013年01月15日 ⁄ 综合 ⁄ 共 3260字 ⁄ 字号 评论关闭

利用系统的环境变量。
对于头文件的搜索路径:

C_INCLUDE_PATH=<your include path>;

 CPLUS_INCLUDE_PATH=<your include path>
export C_INCLUDE_PATH

export CPLUS_INCLUDE_PATH

对于库文件的搜索路径:

LIBRARY_PATH=<your lib path>;
export LIBRARY_PATH

对于链接程序ld使用的库文件搜索路径:

LD_LIBRARY_PATH=<your ldlib path>;
export LD_LIBRARY_PATH

LD_LIBRARY_PATH is
searched when the
program starts,  程序启动时,搜寻库。

LIBRARY_PATH is
searches at link time.  
链接时搜索库。


LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that need to be linked to your program.

编译前。。搜索需要链接的库的路径。

LD_LIBRARY_PATH is used by your program to search for directories containing the libraries after it has been successfully compiled and linked.

我的程序在编译成功和链接成功之后,搜索

EDIT:As pointed below, your libraries can be static or shared. If it is static then the code is copied over into your program and you don't need to search for the library after your program is compiled and linked. If your library is shared then it needs to
be dynamically linked to your program and that's when LD_LIBRARY_PATH comes into play.

http://www.cnblogs.com/panfeng412/archive/2011/10/20/library_path-and-ld_library_path.html

LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别

LIBRARY_PATH和LD_LIBRARY_PATH是Linux下的两个环境变量,二者的含义和作用分别如下:

LIBRARY_PATH环境变量用于在程序编译期间查找动态链接库时指定查找共享库的路径,例如,指定gcc编译需要用到的动态链接库的目录。设置方法如下(其中,LIBDIR1和LIBDIR2为两个库目录):

export LIBRARY_PATH=LIBDIR1:LIBDIR2:$LIBRARY_PATH

LD_LIBRARY_PATH环境变量用于在程序加载运行期间查找动态链接库时指定除了系统默认路径之外的其他路径,注意,LD_LIBRARY_PATH中指定的路径会在系统默认路径之前进行查找。设置方法如下(其中,LIBDIR1和LIBDIR2为两个库目录):

export LD_LIBRARY_PATH=LIBDIR1:LIBDIR2:$LD_LIBRARY_PATH

举个例子,我们开发一个程序,经常会需要使用某个或某些动态链接库,为了保证程序的可移植性,可以先将这些编译好的动态链接库放在自己指定的目录下,然后按照上述方式将这些目录加入到LD_LIBRARY_PATH环境变量中,这样自己的程序就可以动态链接后加载库文件运行了。

区别与使用:

 

 

开发时,设置LIBRARY_PATH,以便gcc能够找到编译时需要的动态链接库。

发布时,设置LD_LIBRARY_PATH,以便程序加载运行时能够自动找到需要的动态链接库。

利用系统的环境变量。
对于头文件的搜索路径:

C_INCLUDE_PATH=<your include path>;

 CPLUS_INCLUDE_PATH=<your include path>
export C_INCLUDE_PATH

export CPLUS_INCLUDE_PATH

对于库文件的搜索路径:

LIBRARY_PATH=<your lib path>;
export LIBRARY_PATH

对于链接程序ld使用的库文件搜索路径:

LD_LIBRARY_PATH=<your ldlib path>;
export LD_LIBRARY_PATH

LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that need to be linked to your program.

LD_LIBRARY_PATH is used by your program to search for directories containing the libraries after it has been successfully compiled and linked.

EDIT:As pointed below, your libraries can be static or shared. If it is static then the code is copied over into your program and you don't need to search for the library after your program is compiled and linked. If your library is shared then it needs to
be dynamically linked to your program and that's when LD_LIBRARY_PATH comes into play.

利用系统的环境变量。
对于头文件的搜索路径:

C_INCLUDE_PATH=<your include path>;

 CPLUS_INCLUDE_PATH=<your include path>
export C_INCLUDE_PATH

export CPLUS_INCLUDE_PATH

对于库文件的搜索路径:

LIBRARY_PATH=<your lib path>;
export LIBRARY_PATH

对于链接程序ld使用的库文件搜索路径:

LD_LIBRARY_PATH=<your ldlib path>;
export LD_LIBRARY_PATH

LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that need to be linked to your program.

LD_LIBRARY_PATH is used by your program to search for directories containing the libraries after it has been successfully compiled and linked.

EDIT:As pointed below, your libraries can be static or shared. If it is static then the code is copied over into your program and you don't need to search for the library after your program is compiled and linked. If your library is shared then it needs to
be dynamically linked to your program and that's when LD_LIBRARY_PATH comes into play.

抱歉!评论已关闭.