Combining multiple static libraries into one GCC scons

I am creating this library, libmyproject.a I want it to include several other static libraries (libone.a and libtwo.a), so the application that references libmyproject should not be linked to libone and libtwo.

I am new to the GCC chain. With Visual C ++, I would just add all the dependencies, and it would create a lib that also includes all the other libraries I want in it.

How can I accomplish this using GCC?

Bonus question: I use scons to build, is there any way to tell scons to do this? Now it just ignores all the additional libraries that I supply, and only compiles .cpp files to the library.

+3
source share
1 answer

ar (binutils) . Perl script :

#!/usr/bin/perl

use warnings;
use strict;
use File::Temp qw(tempdir);
use File::Basename;
use Getopt::Long;

my %opt;
if (!GetOptions(\%opt,
                "dest=s",
               )) {
  die "Invalid option!";
}


my $tempdir = tempdir( CLEANUP => 1 );

if (!chdir($tempdir)) {
    die "Couldn't change directories to `$tempdir': $!";
}

foreach my $lib (@ARGV) {
    my $subdir = $tempdir . '/' . basename($lib);
    mkdir($subdir) or die "Unable to make $subdir: $!";
    chdir($subdir) or die "Unable to cd $subdir: $!";
    system("ar x $lib");
}
chdir($tempdir) or die "Unable to cd $tempdir: $!";
system("ar cr $opt{dest} */*.o") == 0 or die "'ar cr' failed: $?";
system("ranlib $opt{dest}") == 0 or die "'ranlib' failed: $?";

exit 0;

script SCons:

# this can be put to site_scons/site_init.py
def MergeLibs(env, target, sources):
    """Rapack multiple archive files into one."""
    merge_libs_tool = os.path.join('$TOOLS_DIR', 'merge_libraries.pl')
    lib = env.Command('${LIBPREFIX}' + target + '${LIBSUFFIX}', [merge_libs_tool] + sources,
            '$SOURCE -dest ${TARGET.abspath} ${SOURCES[1:].abspath}')
    return lib

# lib_one = env.StaticLibrary(...)
# lib_two = env.StaticLibrary(...)
# merged_lib = env.MergeLibs('myproject', [libone, libtwo])

, , Python SCons. script, .

+1

All Articles