将多个静态库合并到一个GCC scons中

发布于 2024-11-07 08:55:29 字数 298 浏览 3 评论 0原文

我正在构建这个库 libmyproject.a 我希望它包含其他几个静态库(libone.a 和 libtwo.a),以便与 libmyproject 链接的应用程序不必也与 libone 和 libtwo 链接。

我是海湾合作委员会链的新手。使用 Visual C++,我只需添加所有依赖项,它就会创建一个库,其中还包含我想要的所有其他库。

我如何使用 GCC 来实现这一目标?

一个额外的问题:我正在使用 scons 来构建,有没有办法告诉 scons 这样做? 现在它只是忽略我提供的所有附加库,仅将 .cpp 文件编译到库中。

I'm building this library, libmyproject.a
I want it to include several other static libraries (libone.a and libtwo.a), so that the application that links with libmyproject doesn't have to link with libone and libtwo as well.

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

How can I accomplish this with GCC ?

A bonus question: I'm using scons to build, is there a way to tell scons to do this ?
Right now it just ignores all the additional libs that I supply and only compiles the .cpp files into the library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2024-11-14 08:55:30

您必须使用arbinutils)对归档文件的操作。我使用一个简单的 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 函数来合并库。我更喜欢使用单独的脚本,因此它可以像任何其他构建工具一样从命令行运行。

You have to use ar (binutils) for operations on archive files. I am using a simple Perl script to do the merging:

#!/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;

To invoke the script in 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])

Of course, you can merge libraries using Python function inside SCons. I prefer using a separate script, so it can be run from command line as any other build tool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文