C++ 为什么 Visual Studio 2013 在 C4996 上出错?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20448102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:08:38  来源:igfitidea点击:

Why does Visual Studio 2013 error on C4996?

c++visual-studiovisual-studio-2013

提问by Nikolai

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:

在以前版本的 Visual Studio 中,使用 _sleep 或 strncpy 等函数只会输出警告。最新版本,突然报错:

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.

错误 C4996:“_sleep”:此函数或变量已被更新的库或操作系统功能取代。考虑改用睡眠。详细信息请参见在线帮助。

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?

我知道我可以通过#pragma warning(disable: 4996)在代码的开头添加来禁用它,但是 VS 试图强迫我使用其他功能非常烦人。有什么办法可以禁用这种行为吗?

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

在您询问之前,“将警告视为错误”已禁用,即使我关闭所有警告,它也会出错!

回答by Nikolai

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.

显然,新项目现在默认启用“SDK 检查”,它将这些警告视为错误。要禁用它,请转到项目属性 -> 配置属性 -> C/C++ -> 常规 -> SDL 检查 -> 否。

回答by ????? ????

enter at the beginning of the program:

在程序开头输入:

#pragma warning(disable : 4996)

and that's it.

就是这样。

回答by Peter Tseng

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

您还可以在 C/C++ > 高级 > 禁用特定警告中禁用特定警告编号。

回答by Adam G.

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

项目 ->project_name 属性 -> 配置属性 -> C/C++ -> 预处理器 -> 预处理器定义 -> 编辑...添加行 _CRT_SECURE_NO_WARNINGS

回答by xAditya3393

Compiling all sources I have referred:

编译我提到的所有来源:

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

默认情况下,在 Visual Studio 中从项目中删除安全警告 (_CRT_SECURE_NO_WARNINGS)

kmcnamee's answer on How to use use _CRT_SECURE_NO_WARNINGS

kmcnamee 关于如何使用_CRT_SECURE_NO_WARNINGS 的回答

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

视频解决了我的问题。 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.

显然,安全开发生命周期 (SDL) 推荐的检查包括启用额外的安全代码生成功能和额外的安全相关警告作为错误。

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