67

In previous versions of Visual Studio using functions like _sleep or strncpy just outputs a warning. In the latest version, it's suddenly an error:

unexpected error

error C4996: '_sleep': This function or variable has been superseded by newer library or operating system functionality. Consider using Sleep instead. See online help for details.

I know I can disable it by adding #pragma warning(disable: 4996) in the beginning of the code, but it's extremely annoying that VS is trying to force me to use other functions. Is there any way to disable this behavior?

Before you ask, "Treat Warnings As Errors" is disabled, and it errors even if I turn off all warnings!

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Nikolai
  • 3,053
  • 3
  • 24
  • 33
  • possible duplicate of [Error C4996 received when compiling sqlite.c in Visual Studio 2013](http://stackoverflow.com/questions/20031597/error-c4996-received-when-compiling-sqlite-c-in-visual-studio-2013) – dns May 25 '14 at 15:49
  • You could also use a [stdext::checked_array_iterator](http://stackoverflow.com/questions/3598257/visual-c-how-is-checked-array-iterator-useful), but it seems to be non-standard. – jliv902 May 28 '14 at 14:19
  • _CRT_NONSTDC_NO_DEPRECATE worked for me in VS 2017. – Nick Westgate Apr 12 '18 at 10:27

6 Answers6

97

Apparently new projects enable "SDK check" by default now, which treats these warnings as errors. To disable it, go to project properties -> Configuration Properties -> C/C++ -> General -> SDL checks -> No.

Nikolai
  • 3,053
  • 3
  • 24
  • 33
  • 2
    Disabling SDL checks didn't fix the warning for some deprecated Windows API functions I ran into, like [`GetVersionEx`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724451.aspx). Had to disable warning 4996 specifically to fix those. – Nathan Reed Sep 25 '14 at 22:29
  • Wow thanks much for the guy who asked this question and the one who answers. It fixed the error for me – RaHuL Apr 06 '21 at 15:07
12

enter at the beginning of the program:

#pragma warning(disable : 4996)

and that's it.

Nitay Derei
  • 163
  • 2
  • 9
5

You can also disable specific warning numbers in C/C++ > Advanced > Disable Specific Warnings.

Peter Tseng
  • 13,613
  • 4
  • 67
  • 57
3

Compiling all sources I have referred:

Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio

kmcnamee's answer on How to use use _CRT_SECURE_NO_WARNINGS

Video that solved my problem. https://www.youtube.com/watch?v=qWxGZLjwKL0

Apparently, Security Development Lifecycle (SDL) recommended checks which include enabling additional secure code generation features and extra security-relevant warnings as errors.

The steps to solve this issue are:

1. Go to Project-> "your project name" Properties
2. Under Configuration Properties, go to C/C++
3. Under C/C++, go to Preprocessor 
4. Select Preprocessor Definitions and click on Edit from the dropdown menu
5. In the blank space fill out _CRT_SECURE_NO_WARNINGS
xAditya3393
  • 149
  • 4
  • 12
3

Just to add to this, _CRT_NONSTDC_NO_DEPRECATE worked for me in VS2019. _CRT_SECURE_NO_WARNINGS alone did not clear this for me (I have both defined).

Similar to the other answers, this may be added by right-clicking the project in Solution Explorer, Then going to Properties->Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions->Edit... then adding the line _CRT_NONSTDC_NO_DEPRECATE.

Travis Vroman
  • 364
  • 2
  • 9
2

Project ->project_name properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions -> Edit... add line _CRT_SECURE_NO_WARNINGS

Adam G.
  • 81
  • 1
  • 10