linux scons ld_run_path,scons可执行文件+项目目录中的共享库

Here's a sample SConscript file:

这里有一个示例SConscript文件:

env = Environment()

hello_lib = env.SharedLibrary('hello', ['libhello.c'])

exe = env.Program('main', ['main.c'], LIBS=hello_lib)

env.Install('/usr/lib', hello_lib)

env.Install('/usr/bin', exe)

env.Alias('install', '/usr/bin')

env.Alias('install', '/usr/lib')

It builds one shared library, and one executable linked to that library:

它构建一个共享库,以及一个链接到该库的可执行文件:

$ scons

scons: Reading SConscript files ...

scons: done reading SConscript files.

scons: Building targets ...

gcc -o libhello.os -c -fPIC libhello.c

gcc -o libhello.so -shared libhello.os

gcc -o main.o -c main.c

gcc -o main main.o libhello.so

scons: done building targets.

Now, the issue is the created executable will not find the shared library when running it from the project directory, which is quite natural, since neither the LD_LIBRARY_PATH env variable is set, or any RPATH is set in the executable:

现在的问题是,创建的可执行文件在从项目目录运行时不会找到共享库,这很自然,因为既没有设置LD_LIBRARY_PATH env变量,也没有在可执行文件中设置任何RPATH:

[fedora 00:07:10 2 ~] $ ./main

./main: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory

I can always set the LD_LIBRARY_PATH variable while developing, but this becomes cumbersome if the project has a directory hierarchy with several shared libraries in sub_directories.

在开发过程中,我总是可以设置LD_LIBRARY_PATH变量,但是如果项目有一个目录层次结构,子_directory中有几个共享库,那么这就变得很麻烦。

The GNU autotools/libtool solves this by automagically set the RPATH of the executable to wherever the shared libraries are built in the project directory, which allows for easy running/testing the executable while developing. And it relinks the executable when installing it to leave out those RPATH which arn't needed anymore.

GNU autotools/libtool通过自动地将可执行文件的RPATH设置为在项目目录中构建共享库的位置,从而解决了这个问题。当它安装的时候,它会重新链接可执行文件,从而忽略那些不再需要的RPATH。

Is there anything similar to what autotools does that can be done with scons to ease testing the executables while developing ?

是否有类似于autotools的功能,可以在开发时使用scons来轻松测试可执行文件?

Is there any recommended way to build applications using shared libraries with scons, that makes it easy to run the executable from the build directory ?

有什么推荐的方法可以使用scons的共享库构建应用程序,从而使从构建目录运行可执行文件变得更容易?

2 个解决方案

#1

2

You could modify each of the SConscript files which produce libraries, like so:

您可以修改生成库的每个SConscript文件,如下所示:

hello_lib = env.SharedLibrary('#/lib/hello', ['libhello.c'])

All of your shared libraries are now located in a single directory.

所有共享库现在都位于一个目录中。

The SConscript which produces an executable becomes:

生成可执行文件的SConscript变为:

exe = env.Program('main', ['main.c'], LIBPATH='#/lib', LIBS=hello_lib)

Then you will be able to set LD_LIBRARY_PATH to $PWD/lib.

然后可以将LD_LIBRARY_PATH设置为$PWD/lib。

#2

0

It looks like you are looking for the RPATH option in scons.

看起来您正在scons中寻找RPATH选项。

From the wiki page, the RPATH is described as scons as the following.

在wiki页面中,RPATH被描述为scons。

A list of paths to search for shared libraries when running programs. Currently only used in the GNU linker (gnulink) and IRIX linker (sgilink). Ignored on platforms and toolchains that don't support it. Note that the paths added to RPATH are not transformed by scons in any way: if you want an absolute path, you must make it absolute yourself.

运行程序时搜索共享库的路径列表。目前只在GNU链接器(gnulink)和IRIX链接器(sgilink)中使用。在不支持它的平台和工具链上被忽略。注意,添加到RPATH的路径不是由scons以任何方式转换的:如果您想要一个绝对路径,您必须让它自己成为绝对路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值