【发布时间】:2013-02-25 16:57:44
【问题描述】:
我有问题将 libgcc 链接到静态链接的 .so 中
只有在将 64 位模块与 -m64 链接时才会发生这种情况
Ubuntu 64 位 12.10 gcc 4.7
在 Ubuntu 64bit 12.04 gcc 4.6 上也失败了
32位没问题
$gcc -fPIC -c -o hello.o hello.c -m32
$gcc -shared -m32 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic -lc
$ ldd libhello.so
statically linked
64位失败
$ make
gcc -fPIC -c -o hello.o hello.c
gcc -shared -m64 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic -lc
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a(iofclose.o): relocation R_X86_64_32 against `__gcc_personality_v0' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [libhello.so] Error 1
你好.c
#include <stdio.h>
int f(){
FILE *out = fopen("/tmp/x.log", "wb");
fclose(out);
return 1;
}
生成文件
all: libhello.so
libhello.so: hello.o
gcc -shared -m64 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic -lc
hello.o: hello.c
gcc -fPIC -c -o hello.o hello.c
clean:
rm -f hello.o libhello.so
【问题讨论】:
-
根据this article,这意味着“libc.a”没有使用-fPIC编译,但必须静态链接到-fPIC库中。
-
如果你是在 64 位机器上编译,你不能省略 -m64 标志吗?这会有帮助吗?
-
@RandallCook 同样的错误
-
这个问题被标记为无效@ppa bugs.launchpad.net/ubuntu/+source/gcc-4.7/+bug/1132899