clang++ is awesome, goodbye g++

I finally got around to reading up on the clang compiler and I love it. The error messages are so helpful. Here’s a quick example with a syntax error. Lets compile it on the command line.

#include <iostream>
int main() {
	std::cotut << "Hello World";
	return 0;
}


What happens when I run the code with g++?

paulsolt@~/dev $ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:3: error: ‘cotut’ is not a member of ‘std’

Here’s what happens in clang++, notice how it highlights the position and it’s actually more helpful in fixing the error.

paulsolt@~/dev $ clang++ test.cpp
test.cpp:4:7: error: no member named 'cotut' in namespace 'std'; did you mean 'cout'?
        std::cotut << "Hello World";
        ~~~~~^~~~~
             cout
/usr/include/c++/4.2.1/iostream:63:18: note: 'cout' declared here
  extern ostream cout;          ///< Linked to standard output

8 Comments

  1. Looks good! How do we install clang++?

  2. It’s in the upcoming editor from Apple. See the open source site: http://clang.llvm.org/

  3. Hi,
    I have one query related to clang compiler.
    It gives error for static_assert statement whereas g++ used to build this type of statements succefully…
    Do you have any idea regarding
    this?

  4. I’m not a g++ expert. The new compiler might give more warnings/errors by default.

  5. So… instead of using a basic IDE to catch a syntax error, you rely on the compiler.

    Can only imagine how bad it could get with an extremely bad program… say hello to the new “Microsoft Paperclip” ;p

    *Also, no clue what clang even offers, but g++ boys seem to love pointing out its inadequacies.

  6. The message I pasted is what you’d see in Xcode, you don’t have to be on the command line.

    You’re missing the point. Clang does things that g++ could never do. g++ was under restrictions due to licensing that made things impossible for helpful error messages in Xcode. clang++ is a huge improvement because it was designed to provide verbose and friendly error messages.

    I hardly ever just use the compiler without Xcode. The benefit is that any output from the new compiler/static analyzer is feed into Xcode, which makes Xcode incredibly helpful for a beginner programmer. The old C++ template error messages are no more.

  7. I believe that both are exceptionally clear error messages. Neither compilers will fix the error itself, but they both clearly provide a line number, which is really all that is needed (most of the time) to find the error.

    I do dislike the scare tactics used by G++, but it does make us better programmers.

  8. It’s very daunting to beginners and is a big reason why there’s not more programmers.

    Programming shouldn’t be about scary messages. It should be clear when there’s a mistake and there should be advice on how to fix the mistakes.

    I think you can still be a good programmer, and you have a better chance of sticking to it, if you don’t hit insurmountable walls. I’ve learned a lot from mistakes. What’s the difference between learning from a mistake that requires google vs. one that the compiler can help you work through?

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2014 Paul Solt

Theme by Anders NorenUp ↑