What is the difference? clang++ | clang -std=c++11

30,638

Solution 1

Technically, neither of the programs named clang or clang++ is a compiler: they are both drivers that analyze the input arguments and determine what compilers/assemblers/linkers to invoke on what files with what command line arguments. The only difference between the two is that clang links against only the C standard library if it performs a link, whereas clang++ links against both the C++ and C standard libraries.

The -x=<language> option overrides the driver programs' heuristics for determining source file language, it directs the driver to invoke the compiler for <language> regardless.

The -std=<dialect> option picks which dialect of a particular language you want to use. If you need to ensure that your C++ program is portable to an old C++98 compiler, you can compile it with -std=c++98. -std only applies to the target language: it won't try to compile e.g. assembler or java as C++98, only source files that the driver believes to be C++.

In short, there are two different driver programs to make it easy to select which libraries to link against. There are reasonable use cases for compiling C++ but not linking against the C++ standard library.

Solution 2

Clang is the name of the whole compiler.

However, from a command-line point of view:

  • Clang is the C compiler
  • Clang++ is the C++ compiler (like g++ is a C++ compiler, whereas gcc is a C compiler)

The -std=c++11 option enables the new C++11 standard (as in g++).

Share:
30,638

Related videos on Youtube

djwbrown
Author by

djwbrown

Updated on November 19, 2020

Comments

  • djwbrown
    djwbrown over 3 years

    I had been erroneously using this command, which failed at the link step:

    $ clang -std=c++11 -stdlib=libc++ myInputFile.cpp

    Can anyone explain why clang provides a C++ language option, and why it fails to link? Why don't the options -x c++ or -std=c++11 accomplish the same thing as clang++? Thanks!

    • djwbrown
      djwbrown over 10 years
      What's up with the downvotes? Yes, I understand that clang++ is just a link to configure clang for C++. That's not my question, please look again. If it's so simple please just answer rather than downvote.
    • Pascal Cuoq
      Pascal Cuoq over 10 years
      This question in its 4th revision is a perfectly valid question that I do not see any reason to vote to close or to downvote. It is a “Why…?” question but its scope is small enough that there is hope a generally agreed-on rationale will be offered as an answer.
    • Eric Postpischil
      Eric Postpischil over 10 years
      Should std=c++11 stdlib=libc++ in the question be -std=c++11 -stdlib=libc++?
    • Eric Postpischil
      Eric Postpischil over 10 years
      What error message do you get when you try to link? Are you linking with the command in the question or a different command?
    • djwbrown
      djwbrown over 10 years
      @EricPostpischil You are correct; that's what I get for not copy/pasting. I've edited the question one last time to reflect that.
  • djwbrown
    djwbrown over 10 years
    But, why offer a C++ option flag with the C compiler? That's my main question.
  • mah
    mah over 10 years
    To remain compatible with the way gcc does things.
  • Benjamin Bannier
    Benjamin Bannier over 10 years
    @djwbrown: clang will likely figure out when it runs on C++ code and successfully produce an object file. The difference between clang and clang++ shows up when it tries to link your object files where C++ requires a different set than C. clang will try to link C, and clang++ C++ code.
  • djwbrown
    djwbrown over 10 years
    @BenjaminBannier that helps, but why bother giving you the -x or -std= flags if they don't change the linker to the correct language as well?
  • LIU Qingyuan
    LIU Qingyuan over 3 years
    It is similar to gcc vs g++, isn't it?