gcc中的-Wl,rpath=<your_lib_dir>选项

gcc编译链接动态库时,很有可能编译通过,但是执行时,找不到动态链接库,那是

因为-L选项指定的路径只在编译时有效,编译出来的可执行文件不知道-L选项后面的值,

当然找不到。可以用ldd <your_execute>看看是不有 ‘not found’在你链接的库后面,

解决方法是通过-Wl,rpath=<your_lib_dir>,使得execute记住链接库的位置

gcc -o foo foo.c -L/usr/local/lib -lfoo -Wl,-rpath=/usr/local/lib
gcc -o foo foo.c -L$(prefix)/lib -lfoo -Wl,-rpath=$(prefix)/lib
 

Re: linking with -Wl,-rpath and $(prefix)

  • From: "Nelson H. F. Beebe" <beebe at math dot utah dot edu>
  • To: gcc-help at gcc dot gnu dot org
  • Cc: beebe at math dot utah dot edu, "David Carter-Hitchin" <david at carter-hitchin dot clara dot co dot uk>
  • Date: Fri, 2 Dec 2005 17:18:18 -0700 (MST)
  • Subject: Re: linking with -Wl,-rpath and $(prefix)

"David Carter-Hitchin" <david@carter-hitchin.clara.co.uk> asks on
Fri, 2 Dec 2005 23:47:28 -0000 about -Wl,-rpath versus -L.

The -L option supplies a colon-separated library path that is to be
searched at LINK TIME for libraries. Thus

	cc -o foo foo.c -L/usr/local/lib -lfoo

means that either libfoo.a or libfoo.so should be found in either
/usr/local/lib, or elsewhere in the default search patch (in
GNU/Linux, the directories can be listed in /etc/ld.so.conf, and the
cache updated by running /etc/ldconfig).

Whether the .a or .so form of the library is needed is platform
dependent (e.g., IBM AIX uses only the .a form), and also dependent on
compiler options to select dynamic or static linking.  The default is
normally dynamic linking to save disk space and waste CPU time.

However, this means while that the executable foo may have been
successfully linked against a shared library, at RUN TIME, the
run-time loader looks for it in the default search path, possibly
prefixed by a colon-separated list of libraries supplied by the
LD_LIBRARY_PATH variable.

If, in our example, /usr/local/lib is not part of the default path,
then the run-time loader will not be able to find the shared library,
EVEN THOUGH LINKING SUCCEEDED (because of the -L/usr/local/lib
option).

You can check whether shared libraries can be found by running

	env -i ldd foo

(the "env -i" says to ignore any existing environment variables, such
as LD_LIBRARY_PATH).

For example, on one of my systems, I find

	% env -i ldd /usr/local/bin/emacs
		libXaw3d.so.5 =>         (file not found)
		libXmu.so.4 =>   /usr/lib/libXmu.so.4
		libXt.so.4 =>    /usr/lib/libXt.so.4
		...

Notice the "(file not found") line.  That library is actually present
on that system in /usr/local/lib, and I can make it succeed like this:

	% env -i LD_LIBRARY_PATH=/usr/local/lib ldd /usr/local/bin/emacs
	        libXaw3d.so.5 =>         /usr/local/lib/libXaw3d.so.5
        	libXmu.so.4 =>   /usr/lib/libXmu.so.4
		...

Thus, when shared libraries are present in nondefault directories, you
need to supply an additional linker option, usually -R or -Wl,-rpath=,
with a run-time library path.  Our example above becomes for gcc

	gcc -o foo foo.c -L/usr/local/lib -lfoo -Wl,-rpath=/usr/local/lib

In a Makefile, I would write this as

	gcc -o foo foo.c -L$(prefix)/lib -lfoo -Wl,-rpath=$(prefix)/lib

so that the same library path is used at link time as at run time, and
so that the executable file, foo, records that path.  With GNU
autoconf, the normal condition is that prefix is the root of the file
tree into which you install software locally, so the above command is
fairly typical.  Unfortunately, software developers who have
nondefault library search paths often forget to supply the -Wl,-rpath
or -R options in their Makefiles, with the result that the code builds
and runs at their sites, but not at end user sites.

>From notes that I keep:

>> ...
>> Unfortunately, there are at least three incompatible kinds of
>> command-line options that tell the compiler to instruct the linker to
>> save library paths in the executable:
>>
>> 	-Wl,-rpath,/path/to/dir		gcc, g++, FreeBSD, SGI, Sun compilers
>> 	-rpath /path/to/dir		Compaq/DEC, SGI compilers
>> 	-Rdir:dir:dir			Portland Group, Sun compilers
>>
>> Notice that SGI and Sun support two such flavors.
>> ...

In my view, there is clearly brain damage here: (1) compiler writers
should have standardized on the same option name for recording the
run-time library path (I'd vote for -R), and (2) the linker should
really record the run-time library path by default, so that -R would
almost never be needed.

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                    FAX: +1 801 581 4148                  -
- Department of Mathematics, 110 LCB    Internet e-mail: beebe@math.utah.edu  -
- 155 S 1400 E RM 233                       beebe@acm.org  beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe  -
-------------------------------------------------------------------------------

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值