标签:gcc undefined symbols clang
MAC OS X10.10升级前使用的低版本的gcc(好像是4.7.x),正常编译可以完成,不过会出现警告:
couldn‘t understand kern.osversion `14.0.0‘
网友说好像是Xcode版本过低(使用的是Xcode6 beta4),现在正在升级Xcode 6.1。与此同时,使用HomeBrew升级最新的gcc 4.9.1(X86_64)。后来编译时发现gcc老是跑到clang上去!检查发现原来的gcc文件被删,在PATH中靠后的gcc在/usr/bin中被链接到clang中。遂修复gcc4.9.1的连接,然后编译一个简单的c源代码,出现如下错误:
apple@kissAir: c_src$gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.1/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../configure --build=x86_64-apple-darwin14.0.0 --prefix=/usr/local/Cellar/gcc/4.9.1 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-cloog=/usr/local/opt/cloog --with-isl=/usr/local/opt/isl --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --with-pkgversion=‘Homebrew gcc 4.9.1‘ --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --enable-multilib
Thread model: posix
gcc version 4.9.1 (Homebrew gcc 4.9.1)
apple@kissAir: c_src$gcc -std=c11 -o x x.c
Undefined symbols for architecture x86_64:
"start", referenced from:
-u command line option
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
网上搜了一下,有人说是代码中某些函数名写错了,但我这个显然不是这个原因。看错误字面意思可能是少链接了某个启动库导致,可以通过-v比较clang的最后命令行(因为clang不出错),但为了图懒省事还是罢了。于是通过搜索找到这个连接:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54423 (顺面BS某娘和某Bxxg,根本找不到,还是用某哥翻墙啊),其中和我的现象完全相同,遂加上-lcrt1.o,于是错误消失鸟。nm crt1.o发现其中的确有start符号:
apple@kissAir: c_src$nm /usr/lib/crt1.o
0000000000000170 s EH_Frame1
000000000000012a s LC1
000000000000012b s LC2
0000000000000158 s LC3
00000000000001c8 S _NXArgc
00000000000001d0 S _NXArgv
U ___dso_handle
U ___keymgr_dwarf2_register_sections
00000000000001e0 S ___progname
U __cthread_init_routine
0000000000000124 T __dyld_func_lookup
0000000000000022 T __start
U _atexit
00000000000001d8 S _environ
U _errno
U _exit
U _mach_init_routine
U _main
0000000000000110 T dyld_stub_binding_helper
0000000000000188 s func.eh
0000000000000000 T start
但是用clang正常连接无此问题,貌似也不会连接到crt1.o,不知道gcc为啥一定要连crt1.o,而为啥又默认不加上crt1.o?下面是2种编译器的编译后的符号表:
apple@kissAir: c_src$nm x_gcc x_clang
x_gcc:
0000000100001058 S _NXArgc
0000000100001060 S _NXArgv
U ___keymgr_dwarf2_register_sections
0000000100001070 S ___progname
U __cthread_init_routine
0000000100000e68 t __dyld_func_lookup
0000000100000000 A __mh_execute_header
0000000100000d66 t __start
U _atexit
0000000100001068 S _environ
U _errno
U _exit
U _mach_init_routine
0000000100000e70 S _main
U _printf
U _puts
U _sleep
0000000100000e54 t dyld_stub_binding_helper
0000000100000d44 T start
x_clang:
0000000100000000 T __mh_execute_header
0000000100000f00 T _main
U _printf
U _puts
U _sleep
U dyld_stub_binder
并且clang编译后的大小明显小于gcc:
-rwxr-xr-x 1 apple staff 8.4K 10 27 09:54 x_clang
-rwxr-xr-x 1 apple staff 8.6K 10 27 09:53 x_gcc
还在更新Xcode,看更新完gcc默认是否可以编译通过哦!(稍后更新结果)
mac OX 10.10更新gcc 4.9.1后默认无法编译连接的问题
标签:gcc undefined symbols clang
原文地址:http://blog.csdn.net/mydo/article/details/40503409