Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scons executable + shared library in project directory

Tags:

c

linux

scons

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 Answers

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.

like image 144
Robᵩ Avatar answered Oct 17 '25 17:10

Robᵩ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!