为旧机器制作可移植的c++共享库

问题描述 投票:0回答:1

我已经使用开发了代码

g++ -std=c++23
,使用这个新标准的功能。输出是一个共享库,由二进制可执行文件使用,也可以从 python gui 前端调用为后端。我的开发机器有
g++ 13.2.0
,应用程序应该在具有
g++ 9.4.0
并提供
-std=c++17
GLIBC_2.30
最大值的机器上运行。 代码无法在目标机器上编译,并且无法升级目标机器。 我试图使编译的二进制文件(.so 文件和可执行文件)可移植。使用链接器的
-static-libstdc++ -static-libgcc
选项将此代码烘焙到已编译的 .so 和二进制文件中。但是,正如通常所建议的那样,libc 仍然是静态链接的,并且我收到
/lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by libmysharedlib.so)
错误。 我尝试将 libc.so.6 和 libpthread.so 从目标机器复制到编译机器并使用
-nolibc /path/to/target_machine_libs/libc.so.6 /path/to/target_machine_libs/libpthread.so
链接它们,但它不起作用:目标机器的 libc.so.6 缺少一些符号libstdc++ 指的是:
__isoc23_strtol, _dl_find_object, fstat64, __libc_single_threaded, stat, arc4random, lstat
。 (例如我检查了_dl_find_object,它确实在编译机的libc.so.6中定义,但在目标机的libc.so.6中没有定义)。

有什么办法可以做到这一点吗?我非常确定将 libc.so.6 从编译机复制到目标机是行不通的(尤其是在将 .so 文件加载到 python 程序中时,主机的本机 libc 和移植的 libc 会发生冲突吗?)。

软件开发人员如何设法在旧机器上安装他们的软件?天真的猜测是他们用几个旧版本的 g++ 和相关的库来编译它,并选择适合目标机器的版本,但如果他们想使用一个像样的、最新版本的 g++,它就不起作用。使用最新 C++ 方言的新版本编译器的 C++ 库似乎仅在最新版本的 libc 中可用。

[更新] 我给了 musl 交叉编译器一个机会。我在我的系统上使用了相同版本的 g++,按照此处的提示进行操作:https://github.com/crazywhalecc/static-php-cli/issues/345

  • 在包含“5f95b6d042fb37d45c6cbebfc91decfbc4fb493c gcc-13.2.0.tar.xz”的哈希文件夹中添加 gcc-13.2.0.tar.xz.sha1 (不带引号)。

编译得很好。

我正在尝试使用这些选项编译/链接我的共享库:

CXX = /home/barna/progs/musl/musl-cross-make-master/output/bin/x86_64-linux-musl-g++
LD = /home/barna/progs/musl/musl-cross-make-master/output/bin/x86_64-linux-musl-g++

编译:

$(CXX) -fPIC -Wno-interference-size -std=c++23 -pthread -O2 -o XXX.o XXX.C

链接:

$(LD) -shared -o libmylib.so XXX.o --static-libgcc -static-libstdc++ -L/home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib -l:libc.a

编译有效,但在链接时出现以下错误:

/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(strerror.o): relocation R_X86_64_32 against `.rodata.errmsgstr' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(abort.o): relocation R_X86_64_32 against hidden symbol `__abort_lock' can not be used when making a shared object
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(atexit.o): relocation R_X86_64_32 against `.bss.lock' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(exit.o): relocation R_X86_64_32 against undefined hidden symbol `__fini_array_start' can not be used when making a shared object
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(lite_malloc.o): relocation R_X86_64_32 against `.bss.lock' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(inet_ntop.o): relocation R_X86_64_32 against `.rodata.inet_ntop.str1.1' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(fork.o): relocation R_X86_64_32 against `.rodata.atfork_locks' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(system.o): relocation R_X86_64_32 against `.rodata.system.str1.1' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(block.o): relocation R_X86_64_32 against `.rodata.all_mask' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(sigaction.o): relocation R_X86_64_32 against `.bss.handler_set' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(vsnprintf.o): relocation R_X86_64_32S against `.text.sn_write' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(mktemp.o): relocation R_X86_64_32 against `.rodata.mktemp.str1.1' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(lock_ptc.o): relocation R_X86_64_32 against `.bss.lock' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(vmlock.o): relocation R_X86_64_32 against `.bss.vmlock' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(clock_gettime.o): relocation R_X86_64_32 against `.rodata.cgt_init.str1.1' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(intscan.o): relocation R_X86_64_32S against `.rodata.table' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(_Fork.o): relocation R_X86_64_32 against hidden symbol `__abort_lock' can not be used when making a shared object
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(posix_spawn.o): relocation R_X86_64_32S against `.rodata.child' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(vfprintf.o): relocation R_X86_64_32S against `.rodata.pop_arg' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(__init_tls.o): relocation R_X86_64_32 against hidden symbol `__thread_list_lock' can not be used when making a shared object
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: /home/barna/progs/musl/musl-cross-make-master/output/x86_64-linux-musl/lib/libc.a(ofl.o): relocation R_X86_64_32 against `.bss.ofl_lock' can not be used when making a shared object; recompile with -fPIC
/user-data/barna/progs/musl/musl-cross-make-master/output/bin/../lib/gcc/x86_64-linux-musl/13.2.0/../../../../x86_64-linux-musl/bin/ld: final link failed: nonrepresentable section on output

SO 上似乎存在一个相同的问题:musl 无法将 libc.a 链接到共享库

但我不太清楚如何继续。公认的答案是构建 musl 而不启用 asm。这是怎么做到的?这是正确的方法吗?

谢谢你 此致 丹尼尔

g++ shared-libraries portability
1个回答
0
投票

有什么办法可以做到这一点吗?

是的。

  1. 使用与您的目标环境相匹配的 GLIBC 设置 docker 容器(此处为 2.30)。
  2. 在该容器中,构建并安装 GCC-13.2(从源代码)。
  3. 使用刚刚安装的 GCC-13.2,使用
    -static-libstdc++
    -static-libgcc
    编译并链接您的共享库。

软件开发人员如何设法在旧机器上安装他们的软件?

通常通过使用他们想要支持的最旧的 GLIBC 和 GCC 进行构建(在容器中)(如果从源代码构建,GCC 不必太旧)。

附注将

libc.so.6
从目标复制到构建机器无法工作,原因请参见here

P.P.S。使用 Musl 是行不通的 除非你构建了 一切(你的二进制文件,以及

python
)也可以使用 Musl 加载你的共享库。

© www.soinside.com 2019 - 2024. All rights reserved.