I have a question concerning llvm, clang, and gcc on OS X.
What is the difference between the llvm-gcc 4.2, llvm 2.0 and clang? I know that they all build on llvm but how are they different?
Besides faster compiling, what is the advantage of llvm over gcc?
- 9llvm is only a backend, there cant be a standalone llvm compiler ... there are only different frontends for it, like gcc and clang. – smerlin Apr 18 '11 at 20:46
- 5@smerlin: "llvm compiler" is Apple's trade name for clang + llvm: developer.apple.com/technologies/tools/… – Stephen Canon Apr 18 '11 at 20:50
- Other details here: stackoverflow.com/a/26724886/1938163 – Marco A. Nov 10 '14 at 13:18
LLVM originally stood for "low-level virtual machine", though it now just stands for itself as it has grown to be something other than a traditional virtual machine. It is a set of libraries and tools, as well as a standardized intermediate representation, that can be used to help build compilers and just-in-time compilers. It cannot compile anything other than its own intermediate representation on its own; it needs a language-specific frontend in order to do so. If people just refer to LLVM, they probably mean just the low-level library and tools. Some people might refer to Clang or llvm-gcc incorrectly as "LLVM", which may cause some confusion.
llvm-gcc is a modified version of GCC, which uses LLVM as its backend instead of GCC's own. It is now deprecated, in favor of DragonEgg, which uses GCC's new plugin system to do the same thing without forking GCC.
Clang is a whole new C/C++/Objective-C compiler, which uses its own frontend, and LLVM as the backend. The advantages it provides are better error messages, faster compile time, and an easier way for other tools to hook into the compilation process (like the LLDB debugger and Clang static analyzer). It's also reasonably modular, and so can be used as a library for other software that needs to analyze C, C++, or Objective-C code.
Each of these approaches (plain GCC, GCC + LLVM, and Clang) have their advantages and disadvantages. The last few sets of benchmarks I've seen showed GCC to produce slightly faster code in most test cases (though LLVM had a slight edge in a few), while LLVM and Clang gave significantly better compile times. GCC and the GCC/LLVM combos have the advantage that a lot more code has been tested and works on the GCC flavor of C; there are some compiler specific extensions that only GCC has, and some places where the standard allows the implementation to vary but code depends on one particular implementation. It is a lot more likely if you get a large amount of legacy C code that it will work in GCC than that it will work in Clang, though this is improving over time.
- 13To add to this wonderful answer: clang is also a set of libraries (called libclang) you can use for things like code analyzing, autocompletion, syntax highlighting, etc… This is very handy for IDEs. – user142019 Nov 9 '11 at 9:43
- 5When building for MacOS X or iOS, consider that Clang is the software that Apple uses to build all their MacOS X and iOS software including the operating system, and that Clang is what you get automatically, without effort, and what everyone you'd ever ask for help is using. Apple has never supported gcc past gcc 4.2, and doesn't ship any version of gcc anymore. – gnasher729 Feb 23 '14 at 21:00
- 1@gnasher729 Yes, this answer was written 3 years ago, when Apple was still shipping both llvm-gcc and Clang, with llvm-gcc as the default compiler. Times have changed since then. – Brian Campbell Feb 24 '14 at 5:03
There are 2 different things here.
LLVM is a backend compiler meant to build compilers on top of it. It deals with optimizations and production of code adapted to the target architecture.
CLang is a front end which parses C, C++ and Objective C code and translates it into a representation suitable for LLVM.
llvm gcc was an initial version of a llvm based C++ compiler based on gcc 4.2, which is now deprecated since CLang can parse everything it could parse, and more.
Finally, the main difference between CLang and gcc does not lie in the produced code but in the approach. While gcc is monolithic, CLang has been built as a suite of libraries. This modular design allow great reuse opportunities for IDE or completion tools for example.
At the moment, the code produced by gcc 4.6 is generally a bit faster, but CLang is closing the gap.
llvm-gcc-4.2 uses the GCC front-end to parse your code, then generates the compiled output using LLVM.
The "llvm compiler 2.0" uses the clang front-end to parse your code, and generates the compiled output using LLVM. "clang" is actually just the name for this front-end, but it is often used casually as a name for the compiler as a whole.