Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use clang++ with -std=c++11 -Weverything -Werror

I want to compile the following file (temp.cpp):

#include <iostream> 

class Foo {
public:
  Foo() = default;
};

int main(){
  std::cout << "Works!" << std::endl;
  return 0;
}

With the following command: clang++ temp.cpp -o temp -std=c++11 -Weverything -Werror

There is an error:

temp.cpp:5:11: error: defaulted function definitions are incompatible with C++98 [-Werror,-Wc++98-compat]

I understand that there is a warning like c++98-compat and it is part of everything. How can I enable all warnings except c++98-compat? Is there a c++11 compatible flag for -Weverything?

like image 767
Kocka Avatar asked Jan 06 '13 11:01

Kocka


People also ask

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

How do I change my Clang version?

Select the Configuration Properties > General property page. Modify the Platform Toolset property to LLVM (clang-cl), if it isn't already set. Select the Configuration Properties > Advanced property page. Modify the LLVM Toolset Version property to your preferred version, and then choose OK to save your changes.


1 Answers

Actually, you probably do not want all the warnings, because a number of warnings can be considered as being stylistic or subjective and others (such as the one you ran afoul of) are just stupid in your situation.

-Weverything was initially built for two reasons:

  • discovery: it's pretty hard otherwise to get a list of all available warnings
  • black-listing alternative: with gcc, you cherry pick the warnings you wish to apply (white-listing), with -Weverything you cherry pick those you do not wish to apply; the advantage is that when moving over to a new version of the compiler, you are more likely to benefit from new warnings

Obviously, discovery is not really compatible with production use; therefore you seem to fall in the black-listing case.

Clang diagnostics system will output (by default) the name of the most specific warning group that is responsible for generating a warning (here -Wc++98-compat) and each warning group can be turned off by adding no- right after the -W.

Therefore, for blacklisting, you get:

-Weverything -Wno-c++98-compat -Wno-...

And you are encouraged to revise the list of blacklisted warnings from time to time (for example, when you upgrade to a newer compiler).

like image 189
Matthieu M. Avatar answered Sep 23 '22 04:09

Matthieu M.