48

In latest iOS SDK, Apple provides three compiler options: GCC, LLVM with Clang and LLVM-GCC. I understand more or less what these 3 mean, what LLVM and Clang are, and so on. What I don't know is what this means in practice for iPhone developers. Which of these should I use at this point, as of January 2011? Is LLVM mature enough that I can use it safely without stumbling on bugs in it too often? Does switching to LLVM have any other disadvantages? If it does, then does the speed advantage outweigh them? Are there any other reasons to switch except speed?

Kuba Suder
  • 7,587
  • 9
  • 36
  • 39

5 Answers5

37

Update: Because people are still finding this answer, I feel like I should provide a suitable update. By now, I hope it's clear that Clang is absolutely the way to go when programming, with Clang being the default compiler in the newer versions of Xcode and supporting ARC and new and upcoming language constructs (array and dictionary subscripting, literals, etc.). There's almost absolutely no reason to compile with GCC anymore, and for codebases using ARC and new features, using plain GCC is no longer relevant or possible (LLVM-GCC may support these features, but it provides no advantage over Clang now that Clang is completely stable).


By now (with LLVM-2.0 included in the Xcode 4.0 beta), LLVM is mature enough for production code use. It compiles a little quicker than GCC, and produces faster code, so use it whenever you can (pretty much, try to avoid GCC if something better is available). The standard Xcode 3.2.5 install contains LLVM-1.6 (not the latest), so I'd recommend either running some speed tests to see if there's a noticeable difference between GCC and LLVM, or compiling Clang from source and getting the latest version.

Essentially, there's no need for GCC any more, LLVM + Clang is more than enough.

Itai Ferber
  • 28,308
  • 5
  • 77
  • 83
  • So if I'm running Xcode 3 and I don't want to compile from source, LLVM + GCC is the best option? – Kuba Suder Jan 04 '11 at 00:53
  • 1
    No, just stick with LLVM-1.6 and you'll be fine. Again, there's no reason to use GCC at all, even if combined with LLVM. – Itai Ferber Jan 04 '11 at 02:04
  • 1
    I wouldn't go there quite yet, I've seen and have heard of significant issues yet in LLVM generated code (we had an app store submission crash that only was solved by moving the GCC compiler, at the recommendation of Apple). Possibly one more release and it'll be ready for prime time... – Kendall Helmstetter Gelner Jan 04 '11 at 04:07
  • Kendall, interesting to hear the other side of things. I've not yet heard of events like this, but of course, it's probably an edge case (otherwise, Apple wouldn't be pushing for LLVM so hard). Anyway, as soon as Xcode 4 gets released (whenever that'll be) we'll all be up to date with the latest version of LLVM, and everything will be alright... – Itai Ferber Jan 04 '11 at 04:11
  • 2
    There's a reason that Apple's *production* version of Xcode still uses GCC by default and that is probably that Apple does not feel that clang is ready for production applications yet. Well, I say "probably", in fact it is a guess on my part. It might just be that C++ support is not 100%. – JeremyP Jan 04 '11 at 09:51
18

Ok, I think none of the answers below tell the whole story, so here's my take on a response to my question:

  • LLVM compiles code faster than GCC, may create code that runs faster, and the Clang frontend provides more accurate error messages than GCC – so there are definitely reasons for switching;

  • that said, the version provided with latest stable Xcode (LLVM 1.6) is not 100% stable yet, you can run into some minor bugs if you're unlucky. So if you want to be safe, you should either compile the latest LLVM (2.0) from source, or stick to GCC for the next few months;

  • in a few months, probably when Apple releases Xcode 4, LLVM 2.0 will be the version that ships with Xcode by default, and then we should all be able to switch to it safely.

Thanks to everyone who responded, feel free to correct me if I got something wrong.

Kuba Suder
  • 7,587
  • 9
  • 36
  • 39
  • Saying that LLVM generates may code than runs faster than GCC is simply misleading without sufficient pointers to general code benchmarks. – Karthik Kumar Viswanathan Jul 13 '12 at 05:01
  • I just added a response to gather the different points of view and for/against arguments from the other responses - the part about LLVM producing faster code comes from Itai Ferber's response above. Wikipedia has some info too: http://en.wikipedia.org/wiki/LLVM – Kuba Suder Jul 13 '12 at 11:47
  • 1
    LLVM may compile code fast, but the code which runs isn't guaranteed to run faster. I know for a fact that GCC can run unto 190+ compiler passes (including optimization passes on front end and back end) to generate an executable, which is one reason why GCC takes so much time to compile code. – Karthik Kumar Viswanathan Jul 13 '12 at 17:06
  • 1
    In fact, pointers to actual benchmarks give better data than our comments: http://openbenchmarking.org/result/1204215-SU-LLVMCLANG23 – Karthik Kumar Viswanathan Jul 13 '12 at 17:11
9

I have an app that seems to crash on launch on the original iPhone running iOS 3.1.3 when compiled with LLVM 2.0, but runs just fine with LLVM-GCC. I support back to iOS 3.1 so this is fatal. Not sure if there's an interaction between LLVM 2.0 and some specific code I have, but it seems best to avoid LLVM if you need to support iOS 3.x unless you can test thoroughly on legacy devices.


Update: It seems that the issue is with the device hardware rather than iOS version. 1st and 2nd generation iOS devices seems to be affected: Original iPhone, iPhone 3G, and 1st and 2nd gen iPod Touch. I believe this means it's limited to ARMv6 architectures.

Also, running a debug build via Xcode's debugger works fine, while release builds installed via iTunes do not. So it might be an interaction between CPU architecture and optimization level with LLVM 2.0.

But in any case, avoid for now ;)

Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • We had weird bugs just as this one, on either a 1st gen iPhone (3.1.2) and 2nd gen iPod Touch (4.2.1). Had to go back to LLVM-GCC on Xcode 4.0.1 – Rafael Nobre Apr 04 '11 at 17:02
  • 1
    Yeah -- correlating other crash reports, it looks like it was limited to devices with a first-gen CPU: original iPhone, iPhone 3G, and 1st and 2nd gen iPod Touch. If I'm not mistaken, I think this might be an issue with ARMv6 vs ARMv7. Another interesting tidbit: running a debug build via Xcode's debugger works fine in all cases, it's only an issue when a release build is installed via iTunes; this seems to suggest it has something to do with code optimization. – Daniel Dickison Apr 04 '11 at 18:50
  • 1
    The problem occures not only in release build, it also occures in debug with optimization turned ON. Adding GCC_THUMB_SUPPORT to your build settings and setting it to NO will help with this issue – iKiR Apr 13 '11 at 04:41
4

Another major reason to switch to Clang is more accurate (column & line number ranges) and readable error messages.

Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
3

During the latest WWDC10 they made a point of encouraging developers to use the more current LLVM compiler. I forget the exact tract that they covered it in detail - one of the "new in Xcode". Basically they suggest using LLVM-2.0 when possible, otherwise LLVM-GCC and to avoid GCC alone completely.

If you're a registered iOS developer, you can view most of the sessions free at http://developer.apple.com/videos/wwdc/2010/

Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
  • 4
    However it turned out after that, some people (including myself) found some really weird bugs from LLVM only compilation. Not often but it was enough to switch back to GCC for all app store submissions. I still use LLVM for debug builds though. I think early this year LLVM will probably be stable enough to use for submissions. – Kendall Helmstetter Gelner Jan 04 '11 at 04:08