gcc -L rpath 用法

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.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值