您必須使用ar(binutils)對歸檔文件進行操作。我用一個簡單的Perl腳本做合併:
#!/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;
調用腳本中使用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])
當然,你可以使用裏面SCons的Python的功能合併庫。我更喜歡使用單獨的腳本,因此可以像命令行一樣運行其他構建工具。