C++ 如何使用_CRT_SECURE_NO_WARNINGS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22450423/
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-28 00:05:21  来源:igfitidea点击:

How to use _CRT_SECURE_NO_WARNINGS

c++visual-c++visual-studio-2012warnings

提问by vico

I have compile error in my simple MFC window application generated from wizard with several lines of code:

在我的简单 MFC 窗口应用程序中,我有几行代码从向导生成的编译错误:

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.

错误 C4996:“strncpy”:此函数或变量可能不安全。考虑使用 strncpy_s 代替。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。

I set Configuration Properties>>C/C++>>Preporocessor>>Preprocessor Definitions>> _CRT_NONSTDC_NO_WARNINGS

我设置 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_WARNINGSdefinition.

但这没有帮助。我有另一个非常接近的项目,它只在这个地方生成警告,并且没有_CRT_NONSTDC_NO_WARNINGS定义。

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?

为什么 _CRT_NONSTDC_NO_WARNINGS 在第一个项目中没有帮助,为什么第二个项目在没有这个定义的情况下编译没有问题?

回答by nexusclarum

Under "Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions" add _CRT_SECURE_NO_WARNINGS

在“项目 -> 属性 -> C/C++ -> 预处理器 -> 预处理器定义”下添加 _CRT_SECURE_NO_WARNINGS

回答by Balu

Add by

添加者

Configuration Properties>>C/C++>>Preporocessor>>Preprocessor Definitions>> _CRT_SECURE_NO_WARNINGS

配置属性>>C/C++>>预处理器>>预处理器定义>>_CRT_SECURE_NO_WARNINGS

screenshot of the relevant config interface

相关配置界面截图

回答by kmcnamee

If your are in Visual Studio 2012 or later this has an additional setting 'SDL checks' Under Property Pages -> C/C++ -> General

如果您使用的是 Visual Studio 2012 或更高版本,则在属性页 -> C/C++ -> 常规下有一个附加设置“SDL 检查”

Additional Security Development Lifecycle (SDL) recommended checks; includes enabling additional secure code generation features and extra security-relevant warnings as errors.

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

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.

它默认为 YES - 出于某种原因,IE 你应该使用 strncpy 的安全版本。如果将此更改为 NO,则在使用不安全版本时不会出现错误。

SDL checks in vs2012 and later

vs2012 及更高版本中的 SDL 检查

回答by Carlosio

For a quick fix or test, I find it handy just adding #define _CRT_SECURE_NO_WARNINGSto the top of the file before all #include

为了快速修复或测试,我发现#define _CRT_SECURE_NO_WARNINGS在所有 #include 之前添加到文件顶部很方便

#define _CRT_SECURE_NO_WARNINGS
#include ...
int main(){
    //...
}

回答by user2703790

Adding _CRT_SECURE_NO_WARNINGSto Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitionsdidn't work for me, don't know why.

添加_CRT_SECURE_NO_WARNINGS项目 -> 属性 -> C/C++ -> 预处理器 -> 预处理器定义对我不起作用,不知道为什么。

The following hint works: In stdafx.h file, please add

以下提示有效:在 stdafx.h 文件中,请添加

#define _CRT_SECURE_NO_DEPRECATE

before include other header files.

在包含其他头文件之前。