2013-02-25 43 views
3

静态libgcc中我有问题的链接libgcc中为静态链接。所以为什么不能链接64位Ubuntu上

-m64

64位的Ubuntu 12.10 gcc的64位链接模块时4.7

也未能它只发生在Ubuntu 64位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 

的hello.c

#include <stdio.h> 

int f(){ 

FILE *out = fopen("/tmp/x.log", "wb"); 
fclose(out); 

return 1; 
} 

的Makefile

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 
+1

根据[本文](http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=3),这意味着“libc.a”不是使用-fPIC编译,但必须在-fPIC库中静态链接。 – BeniBela 2013-02-25 17:51:01

+0

如果您在64位机器上编译,难道您不能省略-m64标志吗?这会有帮助吗? – 2013-02-26 21:27:31

+0

@RandallCook同样的错误 – farmer1992 2013-02-27 02:11:30

回答

1

答案基本上是 “你不能这样做。”您试图将非PIC代码链接到共享库中,这在x86_64(amd64)体系结构中根本不可能。你需要一个静态的但是lib版本的libgcc,我怀疑这只是你问题的开始。

为什么libgcc通常是共享的原因之一是给定的正在运行的可执行文件必须有一个且只有一个libgcc维护的关键数据结构的副本。静态链接对于最终的可执行文件是有意义的,因为只有一个副本是静态链接到可执行文件的副本,但动态对象的整个要点将被加载到另一个可执行文件中(反过来,它将拥有自己的副本libgcc,共享或静态)。

+2

谢谢。我的问题实际上是我的.so无法在Ubuntu 11.10上加载,因为U 11.10没有libgcc 2.14兼容符号。 – farmer1992 2013-03-19 03:24:38

相关问题