Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the common problems in switching from GCC to Clang?

Tags:

Suppose my C++ is standard-compliant and I don’t rely on third-party libraries, what are the usual pitfalls encountered when replacing “g++” to “clang++” in the makefile? Like incompatible compiler options, different requirement of option order, some other limitations, etc.

like image 898
Leedehai Avatar asked Aug 15 '18 21:08

Leedehai


People also ask

Should I use Clang instead of GCC?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.

Is GCC compatible with Clang?

Yes, for C code Clang and GCC are compatible (they both use the GNU Toolchain for linking, in fact.) You just have to make sure that you tell clang to create compiled objects and not intermediate bitcode objects. C ABI is well-defined, so the only issue is storage format.

Is Clang slower than GCC?

GCC is slower to compile than clang, so I spend a lot of time compiling, but my final system is (usually) faster with GCC, so I have set GCC as my system compiler.


1 Answers

TL;DR: Clang is highly compatible to GCC - just give it a go.

In most cases, Clang could be used as a GCC drop in replacement (clang and clang++ are "GCC compatible drivers"). Clang was designed with GCC compatiblity in mind, and most options available in GCC should also be supported by Clang.

In my experience, of using GCC and Clang interchangeably on several projects, I don't recall any case where Clang failed to compile where GCC succeeded.

However, depending on size and complexity of your project, migration may not be completely smooth. There are several factors that may be impactful, including compiler bugs and different code generation, which may impact application performance and in rare cases even application functionality. Switching compilers is a big change, so once you're able to build successfully, it's a good idea to run all available tests and benchmarks.

Here are just a few examples from SO for GCC and Clang possible incompatibilities. Chances are, you won't encounter such issues.

  • Clang vs GCC vs MSVC template conversion operator - which compiler is right?
  • GCC accepts `constexpr struct {} s;` but Clang rejects it. Who is correct?
  • Why do gcc and clang each produce different output for this program? (conversion operator vs constructor)
  • Inheriting default constructor fails in gcc and works in clang, which one's got the bug?
  • Overload resolution behaviour difference between GCC and clang (SFINAE)

RE: option order - both GCC and Clang accept compiler flags in any order.

like image 65
valiano Avatar answered Sep 28 '22 17:09

valiano