【发布时间】:2018-04-09 09:18:18
【问题描述】:
我已将问题简化为这个最小的 test.c:
#include "png.h"
int function() {
printf("%ld", (long)png_create_read_struct);
}
编译
gcc -shared -fPIC test.c -o test.so -lm -l:libpng16.a
给出错误
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libpng16.a(pngread.o): relocation R_X86_64_PC32 against symbol `png_sRGB_table' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
现在,我发现的这个错误的每个答案都归结为“按照它所说的去做,然后用 -fPIC 重新编译”,但正如你所看到的,我已经在这样做了。那么是什么给出的呢?
(上面的输出来自带有 libpng16 的 Ubuntu 17.10。带有 libpng12 的 Ubuntu 16.04 会导致类似的错误。)
【问题讨论】:
-
不,你还没有这样做。链接器希望
libpng16.a中的对象是 PIC,而它们不是。 这就是它希望你用-fPIC重新编译的东西。 -
为什么不直接链接 libpng 共享库?
-
@JohnBollinger 所以 apt 包中附带的静态库没有为此做好准备,我必须重新编译我想要静态链接的所有内容?该死。有什么方法可以检查 .a 文件是否是用 -fPIC 编译的? (PS请让您的评论成为答案)
标签: c shared-libraries static-libraries ld static-linking