LLVM vs. GCC for iOS development [closed]

2019-01-16 15:37发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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 ;)



回答4:

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



回答5:

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/