MSVC++ warning flags - c++

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

Related

Disable every single gcc extensions (C++)

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

How to enable *all* compiler warnings for major compilers [duplicate]

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.

Difference between -g and -gfull in Clang

The title says it all. I looked everywhere on Clang's docs, but the option -gfull hasn't got any description.
The documentation also lacks information about all of the various debug levels (like -g2), and it would be cool if someone could explain that as well.
Apparently, options -g0, -g1, -g2, -g3 and -gfull in Clang are equivalent to GCC's. I could not find their documentation in Clang, but I found some info about their use in GCC (and they should behave the same in Clang, but I'm not 100% sure):
-gfull is used on Darwin/macOS. Using -gfull is equivalent to -g -fno-eliminate-unused-debug-symbols. Sources: link1 link2.
-g0 to -g3 are debug levels. They specify how much debugging information to include (note that -g0 means no debugging info at all). See here for a more detailed explanation (you find them under the paragraph -glevel).
EDIT:
All these command line options are listed in the Clang manual as GCC-compatible options (source). Therefore, the behavior described in the GCC manual should be the same in Clang.
P.S.: I've already said that in the original answer, but now I added a link to the Clang manual as source.

c++ compiler flags in visual studio

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

Reducing the execution time of the code using CLANG/LLVM compiler

Well... When i was searching for a good compiler I came across clang/LLVM. This compiler gives me same result as other compilers like icc, pgi. But the problem is there are very few tutorials on this compiler... Kindly let me know where can I find the tutorials on the clang compiler.
Note by:
I have compiled my c code using the following flags clang -O3 -mfpmath=sse file.c
Clang (the command line compiler) takes gcc-compatible options, but accepts and ignores a lot of flags that GCC takes (like -mfpmath=sse). We aim to generate good code out of the box. There are some flags that allow clang to violate the language standards that can be useful in some scenarios, like -ffast-math though.
If you're looking for good performance, I highly recommend experimenting with link-time-optimization, which allows clang to optimize across source files in your application. Depending on what platform you're on, this is enabled by passing -O4 to the compiler. If you're on linux, you need to use the "gold" linker (see http://llvm.org/docs/GoldPlugin.html). If you're on the mac, it should "just work" with any recent version of Xcode.
The clang is not a compiler, it is just frontend of LLVM compiler. So, when you calls clang, it parses c/c++ file but the optimization and code generation is handled in LLVM itself.
Here you can found a documentation of LLVM optimization and analysis options: http://llvm.org/docs/Passes.html
The full documentation is here http://llvm.org/docs/
Also useful options are listed here http://linux.die.net/man/1/llvmc (I suggest clang will accept most of them too)