我已将问题简化为最小的 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 会导致类似的错误。)
使用 -fPIC 编译 libpng
user@user_pc:~/Documents$ mkdir libpng
user@user_pc:~/Documents$ cd libpng
user@user_pc:~/Documents/libpng$ wget https://download.sourceforge.net/libpng/libpng-1.6.37.tar.gz
user@user_pc:~/Documents/libpng$ tar xvfz libpng-1.6.37.tar.gz
user@user_pc:~/Documents/libpng$ cd libpng-1.6.37
user@user_pc:~/Documents/libpng/libpng-1.6.37$./configure --prefix=/home/user/Documents/libpng --with-pic=yes
user@user_pc:~/Documents/libpng/libpng-1.6.37$ sudo make
你的二进制文件位于~/Documents/libpng/libpng-1.6.37/lib
,最有趣的是libpng.a
现在是用 -fPIC 编译的。
它还解决了在 Linux 上将 Blender 编译为 Python 模块时出现的问题:
/usr/bin/ld.gold: error: /usr/lib/x86_64-linux-gnu/libpng.a(pngerror.o): requires dynamic R_X86_64_PC32 reloc against 'stderr' which may overflow at runtime; recompile with -fPIC
collect2: error: ld returned 1 exit status
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)