4

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

Community
  • 1
  • 1
Thomas
  • 145
  • 2
  • 7
  • You cannot link a static library against another static library. You can include the contents of a static library in another static library. (Assuming non-windows). You've even linked to an example of the commands needed to do that. – bdbaddog Jan 14 '17 at 20:36

2 Answers2

4

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.
Brady
  • 10,207
  • 2
  • 20
  • 59
  • Hi Brady, i tied env.Append( LINKFLAGS=['-lLibrary1']) as well as .CheckLibWithHeader(...) but the ar-command never changes. Thanks a lot, Thomas. – Thomas Mar 20 '12 at 14:10
  • 2
    Sorry, but I think this does not work. This will create a library with to .a files inside but ld doest not understand them because they are not object files, and will raise an error whenever you try to link with this library ... – thamurath Nov 26 '13 at 10:54
2

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.

Community
  • 1
  • 1
bdbaddog
  • 3,357
  • 2
  • 22
  • 33