Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 947
djwbrown Avatar asked Nov 18 '13 11:11

djwbrown


People also ask

Which version of Clang should I use?

If you have already installed Clang/LLVM you don't need to install the compiler again with the Visual Studio installer. We do recommend that you use the most recent version of Clang to get the best support in the IDE. Older versions may have some limitations.

What is Clang version?

The latest version of Clang is the Clang 12. As of November 2020, it has been built with full support for all published C++ standards and partially supports the upcoming C++20 standards.

Does Clang support C ++ 11?

C++11 implementation statusYou can use Clang in C++11 mode with the -std=c++11 option. Clang's C++11 mode can be used with libc++ or with gcc's libstdc++.

What is Clang latest version?

Clang 14, the latest major version of Clang as of March 2022, has full support for all published C++ standards up to C++17, implements most features of C++20, and has initial support for the upcoming C++23 standard.


2 Answers

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.

like image 116
Casey Avatar answered Sep 23 '22 19:09

Casey


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++).

like image 44
Claudio Avatar answered Sep 23 '22 19:09

Claudio