scons executable + shared library in project directory

不想你离开。 提交于 2019-12-12 14:52:15

问题


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.

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

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.

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.

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

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 ?


回答1:


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.

The SConscript which produces an executable becomes:

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

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




回答2:


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

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

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.



来源:https://stackoverflow.com/questions/24564078/scons-executable-shared-library-in-project-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!