What are the pros and cons of GCC vs clang/LLVM?
-
3Pros/Cons for whom? LLVM is clearly superior as backend for compilers, except if you require a ridiculous amount of supported architectures. But I assume you're talking about end users. – Apr 22 '11 at 22:52
-
1Closing questions like these is asinine, and is a massively missed opportunity for Stack Exchange sites like this to host really valuable and insightful content which stays up-to-date--a kind of wikipedia of sorts for programmers. – Gabriel Staples May 01 '20 at 21:59
-
1Related: [Clang vs GCC for my Linux Development project](https://stackoverflow.com/questions/8205858/clang-vs-gcc-for-my-linux-development-project) – Gabriel Staples May 01 '20 at 22:01
4 Answers
Clang has much better error reporting. E.g. if you make a typo in a function name you call, Clang will report that this is likely a typo (and suggest the correct name), while GCC will complain about unknown function name.
GCC usually produces faster code if you target x86/x86-64. For ARM the situation is ambiguous, often Clang optimizes better. Also, AFAIK Clang does not support optimization for code size.
GCC uses a lot of heuristics. This is good for performance (in a typical case), but awful if you want to do some source-level optimization (e.g. loop unrolling). Even small source code changes can make GCC generate a completely different output. Clang is more predictable, and usually it generates the code you expect.
Unlike Jerry Coffin, I find building GCC from source much harder than Clang. The procedure from Clang's Getting Started page always worked for me. Similar manual for GCC never worked in the first try. GCC has dependencies on particular versions of GMP, MPFR, MPC, Parma Polyhedra Library, and CLooG, and I needed several iterations to find the versions which would work for a particular GCC release (yes, using the latest versions of those libraries does not work).
I have a feeling that Clang is better tested than GCC. Even though I only use official releases of GCC, it sometimes produced faulty code. For Clang I usually use the trunk version (again, because it is easy to build), but I never saw it generating wrong output.
GCC is nearly standard in Linux world, and it adds a lot of non-standard features which are widely used in Linux software. Clang tries to be compatible with GCC, but sometimes it just quietly ignores them. Most importantly, Clang does not support OpenMP. However, it also has extensions which are not supported by GCC, but can be useful (e.g. add-with-carry intrinsic functions __buildin_addc).
If you want to do compiler research, or just curious about how it works, you will find Clang/LLVM source code more accessible. Clang/LLVM code is human-readable, not just compiler-readable.
(AINAL declaimer applies) Clang/LLVM license gives you more freedom about what you can do with the code, e.g. use in commercial or closed-source product. License for GCC runtime libraries adds another layer of restrictions while Clang compiler runtime (compiler-rt library) is under permissive MIT license.
Summary: compile with Clang when you develop the program, and with GCC for the final build (but make sure that its faster and doesn't break). Stick with Clang/LLVM if you do compiler research.

- 321
- 2
- 5
-
Interesting, I've never known that Clang has add-with-carry intrinsic. But it should be `__builtin_add`, not `__buildin_addc`. Anyway, Clang is smart enough to generate `addc` when it encounter a wide add using comparison as carry – phuclv Mar 12 '15 at 08:38
-
-
Performance comparisons here are out of date. According to more recent benchmarks, clang is faster than gcc on x86/64 in slightly over half tests (e.g. https://openbenchmarking.org/result/1605071-HA-GCCCLANG568) – Jules Jun 10 '16 at 07:32
gcc is very mature, easy to install (at least for most systems), and is the default compiler for lots of systems so in many cases people have it installed almost without even realizing it. Its been ported to a zillion architectures, so it's much more likely to work out of the box if you have to target obscure hardware.
clang is much newer. It frequently produces much better error messages, especially for C++ templates. In many cases, it runs a lot faster. It's built primarily as a set of libraries, so there are quite a few other projects (e.g., code analyzers) that use the same front-end, understand the same inputs, etc. Using llvm as its back end gives it some extra flexibility in how you generate code from it.

- 44,385
- 5
- 89
- 162
If you have a new CPU architecture or new optimizations, and want to open source them under the GPL, you can contribute them to gcc. However, gcc is less modular, so this may involve more work to add one's modifications/additions. If you want to contribute new architectures or optimizations to an open source project, but under a non-GPL license, you can contribute them to the LLVM/clang. There exist corporations whose legal departments only allow the latter.

- 7,938
- 4
- 21
- 47
Today (8/11/2011), GCC supports a lot more of the C++0x features than Clang does. If you want those features, it's a no-brainer; GCC is your option.

- 5,075
- 25
- 26
-
1This answer is accurate and informative within the time frame specified according to my research. – May 17 '12 at 07:35
-
8But is not a very good answer really, because as of today (28/07/2012) it's point is no longer valid. Clang supports more than GCC, if anything. – DeadMG Jul 28 '12 at 01:52