Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link static library against another static library

I am trying to link a static library [1] into another static library [2] with scons.

Unfortunately the emitted call to "ar" never contains any path to library [1].

According to this post How to merge two "ar" static libraries into one it should be possible to merge to archives into one.

Is the usual call to CheckLibWithHeader not sufficient here?

Best regards

like image 707
Thomas Avatar asked Nov 01 '25 08:11

Thomas


2 Answers

Have you tried specifying the complete path to library [1] when referring to it with the SCons ar command?

Brady

Adding more info to my original answer: Since you havent posted your SCons scripts, I'll assume its something like the one I present below:

Normally, the LIBPATH construction variable is used to specify paths to libraries, but that appears to only work with the Program() builder and is not used with the ar command. What needs to be done then is to specify the complete path for the library in question. Assuming I have the following directory structure:

# tree .
.
|-- SConstruct
|-- fileA.cc
|-- fileA.o
|-- libB
|   `-- libmoduleB.a
|-- libmoduleA.a
`-- libmoduleC.a

Here is the SConscript that shows how to do so:

env = Environment()
env.Library(target = 'moduleA', source = 'fileA.cc')
env.Library(target = 'moduleC', source = ['libmoduleA.a', '#libB/libmoduleB.a'])

Or Instead of the relative dir '#libB', you could specify an absolute path. (the '#' in a path means its relative to the SConscript)

And, to make it portable, you should specify the moduleB library (and moduleA) like this:

libBname = "%smoduleB%s" % (env['LIBPREFIX'], env['LIBSUFFIX'])
libB = os.path.join(pathToLibB, libBname)

Here is the result:

# scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o fileA.o -c fileA.cc
ar rc libmoduleA.a fileA.o
ranlib libmoduleA.a
ar rc libmoduleC.a libmoduleA.a libB/libmoduleB.a
ranlib libmoduleC.a
scons: done building targets.
like image 88
Brady Avatar answered Nov 03 '25 14:11

Brady


You'd need to create a builder which runs the commands in the other SO question you've linked to.

ar -x libabc.a
ar -x libxyz.a
ar -c libaz.a  *.o

Though you might need a scanner to find the files contained in each static library (ar t libabc.a) and then use the output from that as the input to a normal static library builder.

ofiles = env.unArchive('libabc.a')
ofiles.extend(env.unArchive('libxyz.a'))

env.StaticLibrary('az',ofiles)

Something like the above should work.

like image 44
bdbaddog Avatar answered Nov 03 '25 14:11

bdbaddog



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!