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

标签 c++ g++ static-linking dynamic-linking

我正在尝试使用 g++ 将静态库 (staticLib.a) 链接到动态库 (dynamicLib.so) 中:

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

我得到了与 here 相同的错误:

/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)。根据link above ,这似乎是强制性的。但是,staticLib.a 是我无法控制的另一个项目的库。

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

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

所以我的问题如下:是否有任何可能的方法将静态库(编译时不使用 -fPIC)链接到动态库?

相关主题:

最佳答案

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

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

关于c++ - g++ 将静态库链接到动态库(不带 -fPIC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41558363/

相关文章:

c++ - ARM7tdmi处理器测试方法

c++ - int * array = new int [size](); 的有效性

c -lz 库链接顺序(对符号 "inflateInit2_"的 undefined reference )

c++ - 没有用于初始化 const std::shared_ptr<> 的匹配构造函数

c++ - operator= 和 C++ 中不继承的函数?

c++ - g++ 的 VLA 扩展在哪里?

c++ - Hadoop Pipes 找不到共享库

openssl - 使用静态链接构建 libcurl 到 openssl

c++ - 在 C++ 中对某种列表进行排序的最快方法是什么?

mysql - 找不到 libmysqlclient.a