好久没有写c了,今天和老婆大人一起做了一下微软的网上编程题。发现忘了好多。
由于有运行时错误,所以想用gdb调试一下,发现没有gdb,遂使用homebrew安装,结果安装出错。后来发现错误的原因应该是找不到gcc!可是我一直用gcc编译程序的啊,好奇怪!
这两天vpn不稳定,只能百度,好不容易才发现了原因:
从Mac OS 10.9开始,安装Xcode5时不会再安装gcc了,而系统中的gcc实际上只是一个别名。
$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
$ clang -v
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
可以看到gcc实际上就是clang,而clang对应的调试工具是lldb。可是你既然给clang起了别名叫gcc,你就不能好人做到底给lldb也起一个别名么?
我自己在/usr/bin下边建了一个symbol link,以后就可以方便的使用gdb了。
顺便也看了下clang,LLVM和gcc的关系,有兴趣的各位也可以去搜索一下clang和gcc的优劣,现在看来,对于C语言的支持上,clang应该是占有绝对优势的(当然不是指市场占有率,而是用户体验)。
clang
clang: a C language family frontend for LLVM
The goal of the Clang project is to create a new C, C++, Objective C and Objective C++ front-end for the LLVM compiler.
也就是说clang是一个编译器的前端,主要负责语法,词法分析并生成LLVM可以理解的代码传递给LLVM进行编译。
LLVM
The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Despite its name, LLVM has little to do with traditional virtual machines, though it does provide helpful libraries that can be used to build them. The name "LLVM" itself is not an acronym; it is the full name of the project.
LLVM began as a research project at the University of Illinois, with the goal of providing a modern, SSA-based compilation strategy capable of supporting both static and dynamic compilation of arbitrary programming languages. Since then, LLVM has grown to be an umbrella project consisting of a number of subprojects, many of which are being used in production by a wide variety of commercial and open source projects as well as being widely used in academic research. Code in the LLVM project is licensed under the "UIUC" BSD-Style license.
LLVM则是主要的编译并产生机器码的部分。
gcc
The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Java, Ada, and Go, as well as libraries for these languages (libstdc++, libgcj,...). GCC was originally written as the compiler for theGNU operating system.
其实gcc并不是一个软件,而是一套软件,全称GNU Compiler Collection。相对于clang和LLVM来说,也会分为前端,后端,回想一下编译原理,gcc实际上会首先调用cpp对程序进行预处理,这也就相当于clang所做的工作,然后会调用cc1和ld来进行编译和链接,这就相当于LLVM所做的工作。
这里有一篇文章很好的解释了clang/LLVM的发展历史,水果公司和gcc之间的合作以及矛盾等:
从这篇文章中可以看到他们的组合并不是绝对的,gcc同样也可以和LLVM搭配使用。
参考文献:
1,clang官方网站:
http://clang.llvm.org/index.html
2,LLVM官方网站:
3,gcc官方网站: