g++ 将静态库链接到动态库(不带 -fPIC)

g++ linking a static library into a dynamic library (without -fPIC)

我正在尝试 link 使用 g++ 将静态库 (staticLib.a) 转换为动态库 (dynamicLib.so) 使用:

g++  *.o -Wl,--whole-archive staticLib.a -Wl,--no-whole-archive -shared -o dynamicLib.so

我得到了与 相同的错误:

/usr/bin/ld: staticLib.a(object.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC staticLib.a(object.o): error adding symbols: Bad value collect2: error: ld returned 1 exit status

我阅读了几个主题,但找不到我要找的答案。 staticLib.a 未编译为 位置无关代码 (PIC)。根据,这似乎是强制性的。但是,staticLib.a 是我无法控制的另一个项目的库。

我的第一个想法是提取对象 *.o usingar -x(如 in this second link 所述)。但问题仍然存在,因为对象未使用 -fPIC.

编译

我的第二个想法是创建我自己的 Makefile 以在我的项目中用 -fPIC 重新编译 staticLib.a(我不想弄乱现有的项目)。但我不确定这是一个好方法...

所以我的问题如下:是否有任何可能的方法将静态库(在没有 -fPIC 的情况下编译)link 变成动态库?

相关话题:

So my question is the following: Is there any possible way to link a static library (compiled without -fPIC) into a dynamic one ?

由于提供与位置无关的代码需要编译,因此实际上不可能更改已编译的代码。从理论上讲,您可以从二进制文件中逆向工程源代码并重新编译,但那将是完全无效的解决方案。因此,您必须(重新)编译带有 -fPIC 的原始项目。