clang on 64bit使用-m32编译错误

时间:2014-12-03 08:27:40

标签: c++ c compilation clang

我安装了gcc-multilib和g ++ - multilib但是当我尝试使用clang和-m32编译二进制文件它无法正确找到libgcc位置.. :(
我想解决它。我该怎么办?我是个菜鸟。

root@ubuntu:~# clang -m32 test.c -o test  
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when searching for -lgcc  
/usr/bin/ld: cannot find -lgcc  
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s  
/usr/bin/ld: cannot find -lgcc_s  
clang: error: linker command failed with exit code 1 (use -v to see invocation)  
root@ubuntu:~# find / -name libgcc.a  
/usr/lib/gcc/x86_64-linux-gnu/4.8/x32/libgcc.a  
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a  
/usr/lib/gcc/x86_64-linux-gnu/4.8/32/libgcc.a  
root@ubuntu:~# find / -name libgcc_s.so  
/usr/lib/gcc/x86_64-linux-gnu/4.8/x32/libgcc_s.so  
/usr/lib/gcc/x86_64-linux-gnu/4.8/32/libgcc_s.so  
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so  

root@ubuntu:~# clang -m32 test.c -o test -v
Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
Target: i386-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8.2
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.1
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
Selected multilib: .;
 "/usr/bin/clang" -cc1 -triple i386-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.24 -v -resource-dir /usr/bin/../lib/clang/3.5 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.5/include -internal-externc-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /root -ferror-limit 19 -fmessage-length 174 -mstackrealign -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /tmp/test-7046de.o -x c test.c
clang -cc1 version 3.5 based upon LLVM 3.5 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/bin/../lib/clang/3.5/include
 /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/include
End of search list.
 "/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o test /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32 -L/usr/bin/../lib32 -L/lib/../lib32 -L/usr/lib/../lib32 -L/usr/lib/x86_64-linux-gnu/../../lib32 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib /tmp/test-7046de.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crtn.o
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1 个答案:

答案 0 :(得分:0)

我在64位Linux机器上编译32位静态链接vim时遇到了类似的问题。我设法解决了这个问题。输出如下所示

之前有错误:
gcc -static -L/usr/lib -Wl,--as-needed -o vim objects/buffer.o objects/blowfish.o ... /usr/bin/ld: skipping incompatible /usr/lib/libm.a when searching for -lm /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../libm.a when searching for -lm ...

下载并提取32位静态glibc库后,我仍然遇到类似的错误:
gcc -static -L/tmp/a/usr/lib -Wl,--as-needed -o vim objects/buffer.o objects/blowfish.o ... /usr/bin/ld: skipping incompatible /tmp/a/usr/lib/libm.a when searching for -lm /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../libm.a when searching for -lm ...

链接成功:
gcc -m32 -static -L/tmp/a/usr/lib -Wl,--as-needed -o vim objects/buffer.o objects/blowfish.o ...

请注意,我仅将-m32添加到CFLAGS,而不是LDFLAGS。这导致了我必须解决的这个错误。这是修复后两者的样子:

export CFLAGS="-m32" export LDFLAGS="-m32 -static"