Disable Warnings from Google Test - c++

I have just installed a fresh Visual Studio 16.10.3 and added Google Test to my command console app. Right of the bat, I get the following warnings:
Severity
Code
Description
Project
File
Line
Suppression State
Warning
C26812
The enum type 'testing::TestPartResult::Type' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).
Tests
D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\gtest-test-part.h
62
Warning
C26495
Variable 'testing::internal::Mutex::critical_section_' is uninitialized. Always initialize a member variable (type.6).
Tests
D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h
1804
Warning
C26495
Variable 'testing::internal::Mutex::critical_section_init_phase_' is uninitialized. Always initialize a member variable (type.6).
Tests
D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h
1804
Warning
C26495
Variable 'testing::internal::Mutex::owner_thread_id_' is uninitialized. Always initialize a member variable (type.6).
Tests
D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h
1804
Warning
C26495
Variable 'testing::internal::Mutex::type_' is uninitialized. Always initialize a member variable (type.6).
Tests
D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h
1804
Warning
C26812
The enum type 'testing::internal::Mutex::StaticConstructorSelector' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).
Tests
D:\C++\ConsoleTest\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.4\build\native\include\gtest\internal\gtest-port.h
1804
I tried suppressing the warnings by surrounding #include "pch.h" as described here:
#pragma warning(push, 0)
#include "pch.h"
#pragma warning(pop)
However, that didn't work - instead, I only got an additional warning that
#pragma warning(pop): no matching '#pragma warning(push)' (C4193)
I also read the Broken Warnings Theory. Unfortunately, I have to admit that I do not understand it. I think this is the relevant section:
To suppress the warning in external headers, we need to both specify which headers are external and what the warning level in those headers should be:
cl.exe /experimental:external /external:I some_lib_dir /external:W0 /W4 my_prog.cpp
This would effectively get rid of any warning inside some_hdr.hpp while preserving warnings inside my_prog.cpp.
But where would I add these switches and what would be the correct path variable for googletest?
Thank you in advance for your help!

Instead of
suppressing the warnings by surrounding #include "pch.h"
you should use the warning pragma to surround both header files(gtest-port.h and gtest-test-part.h), which can be found in External Dependencies in Solution Explorer of the Visual Studio project. The detailed methods are as below.
Add the following code to the very beginning and end of gtest-port.h
#pragma warning( push )
#pragma warning( disable : 26495 )
#pragma warning( disable : 26812 )
// Original Code
#pragma warning( pop )
Similarly, add the following code to the very beginning and end of gtest-test-part.h
#pragma warning( push )
#pragma warning( disable : 26812 )
// Original Code
#pragma warning( pop )

Related

Getting a compiler warning in the boost C++ libraries

I am getting this warning when I compile my Visual Studio C++ program:
Warning C26495 Variable 'boost::function_base::functor' is uninitialized. Always initialize a member variable (type.6). Project1 C:\boost\function\function_base.hpp 603
And here is the line in function_base.hpp:
public:
detail::function::vtable_base* vtable;
mutable detail::function::function_buffer functor;
Has anyone ran into this with boost and have you solved it? I tried this, but it gave errors:
mutable detail::function::function_buffer functor{};
Minimal reproducible example:
#include <boost\\algorithm\\string\\find.hpp>
int main()
{
}
This 'minimal reproducible example' gives this error:
Warning C26495 Variable 'boost::function_base::functor' is uninitialized. Always initialize a member variable (type.6).
#define BOOST_VERSION 107500
#define BOOST_LIB_VERSION "1_75"
You probably want to disable "external warnings"
Add your Boost directory to the "External Include Directories" so that find.hpp will use the "External Includes" settings. You can then disable code analysis for this specific warning, or drop the warning level to /W3 for similar warnings.

#pragma warning(disable: 4577) doesn't work in VS2017

I'm disabling the warning C4577 at the top of my C++ file, outsides of any scope:
#pragma warning(disable: 4577)
But I'm still getting a lots of warnings like:
warning C4577: 'noexcept' used with no exception handling mode specified; termination on exception is not guaranteed.
How can I disable the warning only for my file?
Regards,
Christian

How to disable warning C4927 in VS2013 Community

I'm receiving the following warning in my project:
warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while calling the constructor 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(const _Elem *)'
with
[
_Elem=char
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xstring(778) : see declaration of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string'
I understand why it's happening, I'm just unable to suppress it. I've tried adding it to the Disable Specific Warnings list in my project settings, and I've also set my warning level to Turn Off All Warnings (/W0), yet the warning persists. Does anyone have any recommendations on how to hide the message?
You can control Visual Studio warnings directly in code using #pragma warning (https://msdn.microsoft.com/en-us/library/2c8f766e.aspx). If you'd like to silence the warning for a single line, you can put the following immediately before the line:
#pragma warning (suppress: 4927)
line-that-causes-warning-4927
You can also disable it from any point past the #pragma, by using 'disable' instead of 'suppress'. However, as the comments suggest, it's better to actually fix the warning, because it could be causing issues in your program.

Deprecated commands in Visual C++

When building a solution in Visual Studio 2013 of a project, I noticed that I am getting warnings for the following references:
warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings src\core\JCSocket.cpp 77 1
warning C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings src\core\JCSocket.cpp 82 1
warning C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings src\core\JCSocket.cpp 121 1
warning C4996: 'inet_ntoa': Use inet_ntop() or InetNtop() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings src\core\MuninNodeServer.cpp 64 1
warning C4996: 'GetVersionExW': was declared deprecated src\plugins\disk\DiskTimeMuninNodePlugin.cpp 48 1
warning C4996: 'GetVersion': was declared deprecated src\plugins\external\ConsolePipe.cpp 12 1
warning C4996: 'GetVersionExW': was declared deprecated src\plugins\PerfCounterMuninNodePlugin.cpp 56 1
warning C4996: 'GetVersionExW': was declared deprecated src\plugins\uptime\UptimeMuninNodePlugin.cpp 34 1
Whenever I try to change it to the recommended IntelliSense command, it is saying:
IntelliSense: identifier "inet_ntop" is undefined \src\core\MuninNodeServer.cpp 64 31
These errors are telling you what to do. Microsoft is nice like that.
gethostbyname -> getaddrinfo
inet_addr -> inet_pton
inet_ntoa -> inet_ntop
As far as GetVersionExW and GetVersion Microsoft recommends using the appropriate Version Helper Function.
Visual Studio tells you this warning cause you trying to use unsecure function which means includes the body of the function
and the body can obviously contain a signed overflow and there's a prospect to get another compiling errors or perhaps its not supported in the newer library
by the way, its about how you use this function
Simply you can disable the warning by add the following line
#pragma warning( disable : 4996)
at the top of your code
Which disable warning error code 4996 and will work fine without any problems.
Another way:
from the Solution Explorer right click on the project and choose Properties
navigate to Configuration Properties >> C/C++ >> Advanced
at the end just put 4996 in disable specific warning and click Apply >> Ok
Note: I prefer code way at all, It's more good to learn if you're not
programming in visual studio or making a header file for
your library

Can I exclude some specific warnings from "treat warnings as errors" without disabling them?

In my Visual C++ code I want to have /WX - "treat warnings as errors". This makes me deal with each warning, including C4996 - "X was declared deprecated" which I don't want to address - I don't want to change code at the moment and I don't want to disable C4996 so that it remains in the output. So ideally I'd like to have something like:
#pragma warning( ExcludeFromWX:4996)
so that all warnings except this one are treated as errors when /WX is used and this warning is just displayed and compilation continues.
Is it possible to get such behavior?
You might be able to reset the specified warning by using following pragma. I did not test it though and you did not mention trying this:
UPDATE
Changing the warning level should succeed
#pragma warning( 4 : 4996 )
This does not work:
#pragma warning( default : 4996 )