Slashdot Mirror


Developing In C/C++? Why You Should Consider Clang Over GCC (dice.com)

Nerval's Lobster writes: The idea with Clang, a compiler front-end for C, C++, Objective-C++, and Objective-C, and LLVM (a compiler infrastructure) is that you can mix the compiler front-end with a targeted back-end and end up with highly portable and efficient compiler. Clang can perform static analysis of your code, and lets you write tools that give you information about a program. Although many developers prefer developing in C/C++ using GCC, developer David Bolton (in a new Dice article) makes an argument for why you should switch to Clang. While GCC is probably still best when it comes to speed, he argues, Clang is improving release by release, and features tools that developers could find useful.

4 of 255 comments (clear)

  1. Re:Translation by ShanghaiBill · · Score: 5, Informative

    Translation: "network tran--I mean speed is a feature that most of our users don't need, so it's not in our development plan"

    No. Clang produces faster code. What TFA means is that GCC compiles faster. But if slightly faster compiles are that important, just turn off the optimizer, or buy a faster computer. How much time does a modern developer spend waiting for the compiler to finish? For me, it is less than 1%. Far more important is the better error messages, warnings, and static analysis from Clang. Those save me way more time than the speed of the compiler.

  2. Re:Translation by ShanghaiBill · · Score: 4, Informative

    Large applications can take -forever- to compile, even if its not a clean compile.

    Many complaints about slow compiles are due to dysfunctional workflow. You should be using precompiled headers, and set up your makefiles for multiple cores. But most importantly, you should be using TDD to develop and debug code outside of the application. So instead of compiling and linking the entire application repeatedly as you track down a bug, you only integrate after all your unit tests are written and passed in isolation.

    But, anyway, it isn't clear that Gcc actually compiles faster than Clang. Here are some benchmarks, and the results are mixed. Sometimes Clang is faster, sometimes Gcc is faster. For a large app, they would likely be about even in compile time. But Clang would likely generate better code, and almost certainly generate more helpful errors/warnings/analysis.

    I can't see any rational reason to prefer Gcc.

  3. Re:License by dmoen · · Score: 4, Informative

    No, LLVM/clang is all about having a superior architecture to GCC, so that a lot of new applications become possible. One of the key ideas is that the optimizer and code generator are libraries with a C++ API. One cool application of this is that you can use the LLVM library to implement a JIT compiler for your interpreted language: you generate the machine code directly into memory (instead of to a file), then execute it.

    LLVM has many more developers than GCC, and is evolving and improving more quickly than GCC can. This is because of the licence: it turns out that corporations like Apple are more willing to provide developer resources for this open source project if the licence isn't copyleft. For this particular project, this means that the BSD license is more successful than the GPL. Of course, there are other projects for which the GPL produces better results in the real world.

    If you want to make a GPL fork of LLVM just for the pure pleasure of fucking over the original project due their heresy in choosing a license you don't approve of, well, good luck with that.

    --
    I have written a truly remarkable program which this sig is too small to contain.
  4. G++ versus Clang++ by ShakaUVM · · Score: 4, Informative

    >better error messages, warnings, and static analysis from Clang. Those save me way more time than the speed of the compiler.

    This is the truth. Ain't nobody got time to read through a hundred lines of error messages to track down a single bug.

    As a simple test, I wrote the following code which has a simple mistake (using an integer instead of an int array in a range-based for loop):

    test.cc:

    #include
    int main() {
            int x = 0;
            for (int i: x) std::cout constexpr const _Tp* std::end(std::initializer_list)
              end(initializer_list __ils) noexcept"

    2 lines for a single straightforward error in clang++ 3.7 (with colors even!):

    "test2.cc:5:12: error: invalid range expression of type 'int'; no viable 'begin' function available
                    for (int i: x) std::cout i;"

    I currently use g++ with new programmers, and will be switching soon to clang++.

    Plus Clang does cool things like interoperate with things like YouCompleteMe to do real time compiling and error message generation for syntax completion in VIM.