scons的可执行+共享项目目录库 [英] scons executable + shared library in project directory

查看:958
本文介绍了scons的可执行+共享项目目录库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个示例SConscript文件:

Here's a sample SConscript file:

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.

现在,这个问题是从项目目录中运行时,它创建的可执行文件将无法找到共享库,
这是很自然的,因为无论是LD_LIBRARY_PATH环境变量设置,或
任何RPATH坐落在可执行文件:

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:

[fedora 00:07:10 2 ~] $ ./main 
./main: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory

我可以随时设置LD_LIBRARY_PATH变量同时发展,但这个变得麻烦,如果该项目在sub_directories几个共享库的目录层次结构。

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.

GNU自动/ libtool的由自动的设定可执行到哪里共享库建在项目目录中,从而方便了运行/测试的可执行文件,同时开发RPATH解决这个问题。它重新链接安装它离开了那些arn't不再需要RPATH时可执行文件。

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.

有什么类似于自动工具也可以使用scons的缓和测试的可执行文件,同时开发做了什么?

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

有没有推荐的方式来建立使用带有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 ?

推荐答案

您可以修改每一个生产库,像这样的SConscript文件:

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

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

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

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

这会产生一个可执行的SConscript变为:

The SConscript which produces an executable becomes:

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

然后,你将能够LD_LIBRARY_PATH设置为 $ PWD / lib目录

这篇关于scons的可执行+共享项目目录库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆