Visual Studio 2015 won't suppress error C4996 - c++

Visual Studio 2015 Community Edition gives the following error when compiling in debug, but not when compiling in release:
std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
I was able to trace back the source of this error to lines 214 and 242 of this third party library I'm using to write bitmap images. I don't fully understand what is going on in these parts, so I'd rather not mess with it.
I'm trying to disable this error, but Visual Studio won't let me. I've tried the following solutions that were suggested in the documentation, on StackOverflow, or elsewhere:
Add 4996 to the "Disable Specific Warnings" field in Project Settings > Configuration Properties > C/C++ > Advanced.
Add /wd4996 to the "Command Arguments" field in Project Settings > Configuration Properties > Debugging.
Add #pragma warning (disable : 4996) at the top of the offending file, and/or above the offending function.
Add _SCL_SECURE_NO_WARNINGS, _SCL_NONSTDC_NO_WARNINGS, _SCL_OBSOLETE_NO_WARNINGS, _SCL_SECURE_NO_WARNINGS_GLOBAL, and combinations thereof to the "Preprocessor Definitions" field in Project Settings > Configuration Properties > C/C++ > Preprocessor.
Add the definitions from the previous solution with a #define directive to the top of the offending file.
Add the definitions from the previous solution but prefixed with /D or with -D to the "Command Arguments" field.
But none of this fixes the issue for me.
What could possibly be the reason for Visual Studio to keep insisting on displaying this error?

define NO_WARN_MBCS_MFC_DEPRECATION

Disabling warning 4996 has no effect on std::copy warnings. To suppress this warning place the following at the top of your source file:
#define _SECURE_SCL_DEPRECATE 0
#include <algorithm>

Add _CRT_NONSTDC_NO_WARNINGS to preprocessor definitions.

In your stdafx.h:
#pragma warning( push )
#pragma warning( disable: 4996)
#include <algorithm>
#pragma warning( pop )
Worked for me VS2015 update 3

Related

Visual Studio disable warnings for files in specific directories

We have a VS 2019 project which includes external source libraries which we add to our project. For instance we are using WTL (Windows Template Library). During compile time, these libraries produce huge numbers of warnings (particularly for members which do not have default values).
For those files, we would like to completely disable warnings, or at least specify which warnings should be excluded for certain directories, because
We are not going to change that code
There are so many warnings, that we can easily miss some from our own code that we should resolve
I saw a post about some flags that you should be able to pass to disable warnings for "external" files, but I do not see any options in the project settings.
There are several ways to disable the warning:
Project Properties->C/C++->General->Warning Level->select level
Here is the Warning Level:
Turn off all warnings (/W0): Turn off the display of all warning
messages. Level 1 (/W1): Display serious warning messages. Level 2
(/W2): Display level 1 warnings and some less serious warnings, such
as warnings about hidden class members. This is the default warning
level on the command line. Level 3 (/W3): Display level 2 warnings
and some less serious warnings, such as warnings about expressions
that always evaluate to true or false. Level 4 (/W4): Display all
level 3 warnings and informational warnings.
Or you could choose to disable specific warnings in Project Properties->C/C++->Advanced->Disable Specific Warnings
You could use warning pragma.
Syntax:
#pragma warning(
warning-specifier : warning-number-list
[; warning-specifier : warning-number-list ... ] )
#pragma warning( push [ , n ] )
#pragma warning( pop )
Also, you could refer to Microsoft about How to: Enable and Disable Code Analysis for Specific C/C++ Warnings.
To enable or disable a code analysis warning
2.1.Create a header file that lists all the code analysis warnings and their initial state, as shown in the following code:
// WarningState.h
#pragma warning ( default : 6001 )
#pragma warning ( disable : 6011 )
// more warnings here
// end of file
2.2.Include WarningState.h in the application header file. In this case, MyApplication.h represents the header file.
// MyApplication.h file
#include "WarningState.h"
// ...
// end of file
2.3.Include MyApplication.h file in the source code file. In this case, MyApplication.cpp represents the source file.
// MyApplication.cpp file
#include "MyApplication.h"
2.4.To modify the warning state, use the pragma warning-specifier in a .cpp file, as shown in the following code:
// MyApplication.cpp file
#include "MyApplication.h"
#pragma warning ( disable: 6001 )
#pragma warning ( default : 6001 )
To disable all code analysis warnings for included third-party files
Add the following code to your header file.
#include <codeanalysis\warnings.h>
#pragma warning( push )
#pragma warning ( disable : ALL_CODE_ANALYSIS_WARNINGS )
#include <third-party include files here>
#pragma warning( pop )

Why does #pragma warning(push,0) enable additional warnings in MSVC 2015?

MCVE:
#pragma warning(push,0)
#include <QWidget>
#pragma warning(pop)
#include <QVariant>
int main(int argc, char *argv[])
{
}
compiling with Visual Studio 2015 (choose "Qt Application" as project type if it matters) and /W4 i get 2xWarning C4100 (unreferenced formal parameter) for argc and argv which is what i expect and are actually interested in. But in addition i get 5xWarning C4251(class X needs to have dll-interface to be used by clients of struct Y) somewhere inside qvariant.h.
With different included files from Qt i can produce different warnings, so it's not just C4251. I was not able to reproduce it with standard library includes yet. The warnings disappear if i remove/comment the #pragma warning-lines or set the projects /W flag to a lower level. The level pushed seems to have no effect, so e.g. with /W0 and #pragma warning(push,4) i get no warnings.
What i would have expected is, that #pragma warning(push,0) disables all warnings inside <QWidget> and #pragma warning(pop) restores the initial state. Instead, Visual Studio seems to somehow detect that those files are from an external library (or is it something inside the Qt code?), but as soon as there is a push/pop pair the pushed warning-level is ignored and instead the initially set warning level via /W flag is used.
I came across this problem initially when using CMake and cotire, which automatically generates a header file to be used as precompiled header. So in my code i don't use #pragma warning (push/pop) which compiles fine without Qt-Warnings. But the generated file wraps the includes in a push/pop pair and thus generates about 200 Warnings in my project. I'm trying to understand why this happens in order to find a solution which requires me neither to wrap all my manual includes in push/pop nor to maintain a private modification to cotire.

This function or variable may be unsafe. To disable deprecation, use _CRT_SECURE_NO_WARNINGS

I'm working on a C++ DDL, however I get the following issue in some places:
C4996 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
I did try #define _CRT_SECURE_NO_WARNINGS, but the issue remains.
This is the code:
sprintf(szDebugString, "%s: 0x%x (%s%s%i)", ptrName, (DWORD)funcPtr, interfaceName, interfaceVersion.c_str(), i);
You have to define _CRT_SECURE_NO_WARNINGS before #include <Windows.h>.
Alternatively, use the safe version:
sprintf_s(szDebugString, sizeof(szDebugString), "%s: 0x%x (%s%s%i)",
ptrName, (DWORD)funcPtr, interfaceName, interfaceVersion.c_str(), i);
To turn off the warning for an entire project in the Visual Studio IDE:
1- Open the Property Pages dialog for your project.
2- Select the Configuration Properties > C/C++ > Advanced page.
3- Edit the Disable Specific Warnings property to add 4996. Choose OK to apply your changes.
put this define into stdafx.h.
E.g.
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
In my point of view, on a Windows project, it is not a good idea to disable the warning; a better idea is to improve the code. Mute the warning not just keeps this potential code vulnerability unnoticed, but also blinds programmers when introducing other potential code vulnerabilities.
From the docs:
You can turn off the warning for a specific line of code by using the warning pragma, #pragma warning(suppress : 4996). You can also turn the warning off within a file by using the warning pragma, #pragma warning(disable : 4996).
https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(C4996)%26rd%3Dtrue&view=vs-2017

How to use _CRT_SECURE_NO_WARNINGS

I have compile error in my simple MFC window application generated from wizard with several lines of code:
error C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
I set Configuration Properties>>C/C++>>Preporocessor>>Preprocessor Definitions>> _CRT_NONSTDC_NO_WARNINGS
But this does't helped. I have another very close project that generates only warning in this place and it has no _CRT_NONSTDC_NO_WARNINGS definition.
Only difference between projects is several different options in wizard.
Why _CRT_NONSTDC_NO_WARNINGS does not helps in first project and why second project compiles without problems without this definition?
Add by
Configuration Properties>>C/C++>>Preporocessor>>Preprocessor
Definitions>> _CRT_SECURE_NO_WARNINGS
Under "Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions" add _CRT_SECURE_NO_WARNINGS
If your are in Visual Studio 2012 or later this has an additional setting 'SDL checks' Under Property Pages -> C/C++ -> General
Additional Security Development Lifecycle (SDL) recommended checks; includes enabling additional secure code generation features and extra security-relevant warnings as errors.
It defaults to YES - For a reason, I.E you should use the secure version of the strncpy. If you change this to NO you will not get a error when using the insecure version.
SDL checks in vs2012 and later
For a quick fix or test, I find it handy just adding #define _CRT_SECURE_NO_WARNINGS to the top of the file before all #include
#define _CRT_SECURE_NO_WARNINGS
#include ...
int main(){
//...
}
Adding _CRT_SECURE_NO_WARNINGS to Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions didn't work for me, don't know why.
The following hint works:
In stdafx.h file, please add
#define _CRT_SECURE_NO_DEPRECATE
before include other header files.
Visual Studio 2019 with CMake
Add the following to CMakeLists.txt:
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
I was getting the same error in Visual Studio 2017 and to fix it just added #define _CRT_SECURE_NO_WARNINGS after #include "pch.h"
#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
....

Disabling Warnings generated via _CRT_SECURE_NO_DEPRECATE

What is the best way to disable the warnings generated via _CRT_SECURE_NO_DEPRECATE that allows them to be reinstated with ease and will work across Visual Studio versions?
If you don't want to pollute your source code (after all this warning presents only with Microsoft compiler), add _CRT_SECURE_NO_WARNINGS symbol to your project settings via "Project"->"Properties"->"Configuration properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions".
Also you can define it just before you include a header file which generates this warning.
You should add something like this
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
And just a small remark, make sure you understand what this warning stands for, and maybe, if you don't intend to use other compilers than MSVC, consider using safer version of functions i.e. strcpy_s instead of strcpy.
You could disable the warnings temporarily in places where they appear by using
#pragma warning(push)
#pragma warning(disable: warning-code) //4996 for _CRT_SECURE_NO_WARNINGS equivalent
// deprecated code here
#pragma warning(pop)
so you don't disable all warnings, which can be harmful at times.
i work on a multi platform project, so i can't use _s function and i don't want pollute my code with visual studio specific code.
my solution is disable the warning 4996 on the visual studio project. go to Project -> Properties -> Configuration properties -> C/C++ -> Advanced -> Disable specific warning add the value 4996.
if you use also the mfc and/or atl library (not my case) define before include mfc _AFX_SECURE_NO_DEPRECATE and before include atl _ATL_SECURE_NO_DEPRECATE.
i use this solution across visual studio 2003 and 2005.
p.s. if you use only visual studio the secure template overloads could be a good solution.
You can also use the Secure Template Overloads, they will help you replace the unsecure calls with secure ones anywhere it is possible to easily deduce buffer size (static arrays).
Just add the following:
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
Then fix the remaining warnings by hand, by using the _s functions.
Combination of #[macbirdie] and #[Adrian Borchardt] answer. Which proves to be very useful in production environment (not messing up previously existing warning, especially during cross-platform compile)
#if (_MSC_VER >= 1400) // Check MSC version
#pragma warning(push)
#pragma warning(disable: 4996) // Disable deprecation
#endif
//... // ...
strcat(base, cat); // Sample depreciated code
//... // ...
#if (_MSC_VER >= 1400) // Check MSC version
#pragma warning(pop) // Renable previous depreciations
#endif
For the warning by warning case, It's wise to restore it to default at some point, since you are doing it on a case by case basis.
#pragma warning(disable: 4996) /* Disable deprecation */
// Code that causes it goes here
#pragma warning(default: 4996) /* Restore default */
The best way to do this is by a simple check and assess. I usually do something like this:
#ifndef _DEPRECATION_DISABLE /* One time only */
#define _DEPRECATION_DISABLE /* Disable deprecation true */
#if (_MSC_VER >= 1400) /* Check version */
#pragma warning(disable: 4996) /* Disable deprecation */
#endif /* #if defined(NMEA_WIN) && (_MSC_VER >= 1400) */
#endif /* #ifndef _DEPRECATION_DISABLE */
All that is really required is the following:
#pragma warning(disable: 4996)
Hasn't failed me yet; Hope this helps
you can disable security check. go to
Project -> Properties -> Configuration properties -> C/C++ -> Code Generation -> Security Check
and select Disable Security Check (/GS-)
You can define the _CRT_SECURE_NO_WARNINGS symbol to suppress them and undefine it to reinstate them back.
Another late answer... Here's how Microsoft uses it in their wchar.h. Notice they also disable Warning C6386:
__inline _CRT_INSECURE_DEPRECATE_MEMORY(wmemcpy_s) wchar_t * __CRTDECL
wmemcpy(_Out_opt_cap_(_N) wchar_t *_S1, _In_opt_count_(_N) const wchar_t *_S2, _In_ size_t _N)
{
#pragma warning( push )
#pragma warning( disable : 4996 6386 )
return (wchar_t *)memcpy(_S1, _S2, _N*sizeof(wchar_t));
#pragma warning( pop )
}