Compiler Options in Xcode - GCC or LLVM?

转载 2012年03月27日 15:59:11

转自:http://useyourloaf.com/blog/2011/3/21/compiler-options-in-xcode-gcc-or-llvm.html


Chances are if you are using Xcode 3 and you haven’t played with the build settings for a project that you are still using the GNU Compiler Collection, GCC. Apple is slowly phasing out support for GCC and moving to new compiler technologies based on the Low Level Virtual Machine (LLVM) open source project. The LLVM based tools are available (with some differences) to both Xcode 3 and 4 so you do not have to wait until you migrate to Xcode 4 to start taking advantage of it.

The LLVM Project

The LLVM project is an open source collection of tools (see LLVM.org for the full list) that build on a core set of libraries that provide an optimizer and code generator. Other tools in the LLVM project include the Clang frontend parser and the LLDB debugger. Xcode 4 exploits this modular approach to provide features such as improved syntax highlighting and to suggest fixes to common coding errors.

As of writing GCC remains the default compiler for Xcode 3 but with the release of Xcode 4 the default compiler for new projects has changed to LLVM-GCC. In this post I’ll take a look at the options for both Xcode 3 and 4.

Compiler Options in Xcode

You can set the compiler you want to use in Xcode in the build settings for a project or target The compiler options vary slightly between Xcode 3 and 4 as shown below:

  • Xcode 3 Compiler Verisons

 

  • Xcode 4 Compiler Versions

 

There are really three compiler options depending on whether you want to stick with GCC, switch to LLVM or use a hybrid approach for backward compatibility. I am ignoring the GCC 4.0 option as that is now deprecated and is not even present in Xcode 4. The diagram below shows the differences between the three options:

The implications for each of these options varies a little depending on whether you are using Xcode 3 or Xcode 4 and also whether you are targeting iOS or Mac OS X.

Legacy GCC support (GCC 4.2)

If you need to stick with GCC 4.2 you can but Apple has stated that they are no longer fixing bugs in GCC so this is not a long term option. GCC 4.2 is the default compiler for Xcode 3 and for now remains an option in Xcode 4.

LLVM-GCC 4.2

This hybrid option uses LLVM as an optimizer and code generator plug-in to the GCC front end parser. This is now the default compiler in Xcode 4 for new projects but you can also choose it for Xcode 3 projects. The main reason to switch to LLVM-GCC is that it offers both performance gains and faster build times. The improvements to build times though are limited since this option still uses the GCC frontend parser and for debug builds (which are probably what you are building most often) it does not make use of the optimizer.

LLVM compiler

This option makes full of use of the LLVM toolset. It uses the Clang frontend and LLVM backend optimizer and code generator. Apple claims that the Clang parser is 3x faster than GCC for debug builds whilst maintaining compatibility with GCC. However the advantage of using Clang is about much more than just speed. If you have already used the Build and Analyze option in Xcode you will have seen some of the power of Clang. It provides much more precise error and warning messages along with suggestions for how to fix the problem.

For example, if you make a typo when referring to a previously declared variable Clang is smart enough to figure out what you meant and suggest the correction. It then actually assumes the correction has been applied for the rest of the code. This reduces the number of meaningless error messages that you see making it much easier to focus in on the actual error. Xcode 4 makes use of this to provide the Fix-it feature so that you see the suggested fix in your code which you can then with one click accept:

So if I write this:

NSString *myText;

myTest = [[NSString allocinit];

 

Xcode will make use of Clang to suggest the following fix for me:

Differences between iOS and Mac OS X

The LLVM compiler has supported both iOS and Mac OS X since Xcode 3.2.3 (earlier versions only targeted OS X). Until recently it also did not support C++ with Xcode 3 though I believe that limitation no longer exists. So regardless of whether you are developing for iOS or Mac OS X you can make use of LLVM.

One limitation to be aware of is with the LLDB debugger. It is only available with Xcode 4 but currently only supports Mac OS X. You cannot select the LLDB debugger if your working on iOS targets you have to stick with GDB for now.

Which to Choose?

So which of the three options should you choose? The one I would now avoid, unless you have a strong reason otherwise, is plain old GCC 4.2. Apple is no longer fixing it and the LLVM-GCC option looks to be a better choice. Changing compilers for an existing app is a major change so you should test thoroughly of course if you do make the switch.

For new apps the LLVM-GCC option looks to be the safe option. Apple considers it to be stable and mature enough to make it the default choice in Xcode 4 (two words which you may not associate with Xcode 4 itself) and since it uses the GCC parser backward compatibility should be good.

If LLVM-GCC is the safe option I don’t mean to imply that the pure Clang/LLVM solution is less safe, only less mature. I have been using the LLVM Compiler 2.0 option in Xcode 4 to recompile some old code and so far I did not see any issues. If you have tried it and hit a problem I would be interested to hear the details in the comments.


你或许应该知道的LLVM

原文链接 作为iOS或者Mac开发者,你也许非常眼熟LLVM这个字眼,但也许没有太去在意它。在很长的一段时间内,我就是处于这个状态,不知道它背后是在干嘛。随着苹果新语言swift的发布,我看...
  • khlljm
  • khlljm
  • 2016年07月04日 18:57
  • 3150

GCC与LLVM见解

首先的说GCC(GNU Compiler Collection,GNU编译器集合)是一个优秀的编译器 ,但是它跟IDE之间的互操作性不够好,编译器分为编译器前端和编译器后端,前端主要负责展开预处理器宏...
  • u013770512
  • u013770512
  • 2014年03月20日 23:36
  • 835

gcc的几个妙用

gcc的学习在C接触到linux以后就开始不断的学习,也知道了一些基本的用法,但是关于gcc的使用还是有很多值得我们加深的地方。gcc只是一个编译工具而已。也就相当于我们在windows环境下的vis...
  • tspangle
  • tspangle
  • 2014年01月07日 17:32
  • 1347

LLVM与Clang的概述及关系

LLVM是构架编译器(compiler)的框架系统,以C++编写而成,用于优化以任意程序语言编写的程序的编译时间(compile-time)、链接时间(link-time)、运行时间(run-time...
  • talentedlas
  • talentedlas
  • 2016年07月18日 21:07
  • 596

GCC,LLVM,Clang编译器对比

文章来源:http://www.cnblogs.com/qoakzmxncb/archive/2013/04/18/3029105.html 在XCode中,我们经常会看到这些编译选项(如下图)...
  • LANGZI7758521
  • LANGZI7758521
  • 2016年09月09日 16:27
  • 1087

在mac下编译llvm的源码

在mac下编译llvm的xcode工程
  • skylin19840101
  • skylin19840101
  • 2017年05月19日 15:04
  • 597

codeblocks编译出错问题的解答!(编译c++ 或者c程序)

典型错误: execution of 'mingw32-g++.exe ……faile! codeblocks cannot exec `cc1plus': No such file or direc...
  • jingmiaa
  • jingmiaa
  • 2016年07月28日 12:58
  • 4475

iOS开发中使用宏定义提高开发效率 以及配合Compiler Flags写出高质量的代码

iOS开发中,巧妙的使用宏定义,可以提高开发效率,本篇简单介绍一下宏的定义,设置,应用,并在未来实践中不断追加一些常用的宏定义。 调试Log iPhone应用程序开发调试的时候,在代码中加...
  • u012927973
  • u012927973
  • 2016年02月02日 17:01
  • 941

Gcc-GNU C Compiler 编译方法(为知学习笔记)

1、开源+效率,所有选择了它,效率提高20%~30% 2、使用方法:         gcc 【options】 filenames 3、编译hello.c       gcc  hello....
  • wangcheng74520
  • wangcheng74520
  • 2014年03月05日 14:18
  • 490

llvm中如何利用分支概率和基本块频率估计

1. 背景 llvm自2.9版以后,已经集成了对分支概率和基本块频率的静态分析。 分支概率(branch probability)是指在程序的控制流图中,从控制流从一个基本块A到其任意后继基本块Si的...
  • rrerre
  • rrerre
  • 2013年08月09日 11:06
  • 1426
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Compiler Options in Xcode - GCC or LLVM?
举报原因:
原因补充:

(最多只允许输入30个字)