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.
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.
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.
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.
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.
RE: option order - both GCC and Clang accept compiler flags in any order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With