This question already has answers here:
How do I enable all warnings in Visual Studio 2008 in a project?
(1 answer)
How can I turn on (literally) ALL of GCC's warnings?
(8 answers)
Closed last year.
I think we all know how compiler warnings can help prevent bugs and that it's good to have them enabled.
So the question is: how to enable all compiler warnings?
The major C++ compilers for me are:
g++
clang++
Visual Studio's compiler
I am aware of the common settings for these compilers:
-Wall -Wextra -pedantic (common for g++ and clang++)
/W4 (vc++)
However, there are some more warnings that can be enabled more individually (like for example -Wconversion).
Is is possible to enable all warnings without passing a huge number of individual switches? If all is impractical (due to for example lots of warnings generated by standard library headers, or many false positives), how to enable the most useful subset?
I understand "the most useful subset" differs depending on the project, but perhaps there is a good subset each project can start with.
Related
The title is quite clear, is there a way to disable every single non-standard feature of gcc (extension) when compiling some C++ code. I've previously always used -pedantic-errors alongside -Wall and -Wextra, the first according to the gcc man page does the following
Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some
equivalent to -Werror=pedantic, since there are errors enabled by this option and not
particular library's limitations. However, if -Wpedantic is used with -Wformat, warnings
made into an error by -pedantic-errors.
-Wall or -Wpedantic.
As a result it disables practically all extensions that aren't standard conforming, however. I was rather shooked that the following code compiles with gcc
void foo(std::vector<auto>) { ... }
with the -std=c++20 flag, if we bump it down to -std=c++17 we receive an error message prompting the use of -std=c++20 as it's a "c++20 feature". However neither clang or msvc is willing to compile this. After some further research I was notified of the following
This is a GCC extension and accepted by design.
Which is rather upsetting as even the safe-guards of -pedantic-errors, -Wall and -Wextra didn't pick this up, which leads me to question what other extensions might be silently passed through by gcc. Thus the question comes; is there any way to disable exactly every gcc extension?
Edit: From the comment section of How to disable GNU C extensions?
You cannot
I would like to know, is there a MSVC(++) equivalent for the -Weffc++ flag? What number is it?
I didn't see anything like it in the list of compiler warnings/errors on the MS website.
There's not any equivalent.
Visual C++ doesn't have warning groups at all, only the warning level slider, and the ability to disable individual warnings. So none of the GCC/clang warning group options have an equivalent.
Beyond that, the Effective C++ book is rather old and some of its advice is no longer considered to be best practices. So enforcement has minimal value.
Some editions of Visual C++ come with code analysis, which has many more of these checks than the basic compiler. That's turned on using /analyze, documentation is here: https://learn.microsoft.com/en-us/visualstudio/code-quality/code-analysis-for-c-cpp-overview
Strictly equivalent probably not, but starting from Visual Studio 2015 there is C++ Core Guidelines checkers: https://learn.microsoft.com/en-us/visualstudio/code-quality/using-the-cpp-core-guidelines-checkers
I'm taking a MOOC course on data structures and algorithm. I would like to use c++, and I need to set my compiler options to the following
g++ -pipe -O2 -std=c++14 -lm
I'm currently using MS Visual Studio 2017 on Windows. Is it even possible? Do I need to do custom build? The following is the paragraph taken from the MOOC mentions about Windows users having to use Cygwin, but I have no clue what that means. Can anybody shed some light on a feasible way to do this?
Your solution will be compiled as follows. We recommend that when
testing your solution locally, you use the same compiler flags for
compiling. This will increase the chances that your program behaves in
the same way on your machine and on the testing machine (note that a
buggy program may behave differently when compiled by different
compilers, or even by the same compiler with different flags).
C++ (g++ 5.2.1). File extensions: .cc, .cpp. Flags
g++ -pipe -O2 -std=c++14 -lm
If your C/C++ compiler does not recognize the "-std=c++14" flag, try
replacing it with "-std=c++11" or "-std=c++0x" flag or compiling
without this flag at all (all starter solutions can be compiled
without it). On Linux and MacOS, you probably have the required
compiler. On Windows, you may use your favorite compiler or install an
environment such as cygwin.
As they said you should choose cygwin (https://cygwin.com/install.html).
These would be the flags for msvc: /std:c++14 /O2 /Im
You can set compiler options for each project in its Property Pages
dialog box. In the left pane, select Configuration Properties, C/C++
and then choose the compiler option category.
For the categories lookup: https://learn.microsoft.com/de-de/cpp/build/reference/compiler-options-listed-by-category
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm a starter in C++. I'm still confused about the flags in my makefile.
-c. -o. -Wall. -g. -std=c++0x.
Can any one tell me what all these common flags do?
Only the last flag (compiler command option) is very compiler-specific. The first three or four have been de facto standard since, well, the early or mid 1980s, I think. Of course there’s no guarantee that any particular compiler will understand them, but they’re not uncommon.
Note: while Visual C++ accepts the - flag prefix notation, it is usually used with / as flag prefix, since that’s the common convention in Windows.
-c
Compile only, don't link.
-o
Specifies an output file, e.g. executable. Unfortunately deprecated
for Visual C++ compiler. With Visual C++ use e.g. /Fe.
-Wall
With g++ all practical warnings. With Visual C++ all warnings including all sillywarnings, and that's a bunch!
-g
Generate debug information. Supported by many compilers but not Visual C++.
Then,
-std=c++0x
is a g++ compiler-specific option that specifies sort of C++11 standard. As I recall the difference from -std=c++11, for newer compiler versions that accept both, is that the former still permits some g++-specific language extensions.
Those are not "c++ flags", those are flags for a compiler, presumably it's g++ from the gcc suite.
All those flags are documented on both the online and the offline docs, the offline docs are probably easier to browse for a beginner due to the fact that you can just open the pdf in a viewer and use the search engine to search for a word.
This are the manuals for gcc.
With gcc and clang, I routinely use -Wall -Wextra warning flags.
What command line switches for Visual C++ 2010 (and newer, if there are differences) would produce about same result?
This MSDN document page provides the technical details, but I am after a more qualitative answer from Visual C++ developers, based on practical experience: A "rule of thumb" flags you can safely throw on any new project when you start, that would still catch approximately the same things that -Wall -Wextra of the other compilers catch.
I agree with #Paulius's answer here which you linked to in the comments. /W4 will show you all warnings that are not disabled by default, which is probably the closest you can get to "approximately the same things" without searching through each of the warnings individually and enabling the equivalent. /Wall in MSVC will do what it says and turn on all warnings (even the questionable ones), which for any non-trivial project will give false positives. This is different from gcc -Wall, which will only turn on warnings that are not "questionable".