C++ 禁用通过 _CRT_SECURE_NO_DEPRECATE 生成的警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/119578/
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
Disabling Warnings generated via _CRT_SECURE_NO_DEPRECATE
提问by grrussel
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?
禁用生成的警告的最佳方法是什么_CRT_SECURE_NO_DEPRECATE
,允许它们轻松恢复并在 Visual Studio 版本中工作?
回答by Serge
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".
如果您不想污染源代码(毕竟此警告仅在 Microsoft 编译器中出现),_CRT_SECURE_NO_WARNINGS
请通过“项目”->“属性”->“配置属性”->“C/C++”将符号添加到您的项目设置中“->”预处理器”->“预处理器定义”。
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.
只是一个小小的评论,请确保您了解此警告的含义,并且如果您不打算使用 MSVC 以外的其他编译器,请考虑使用更安全的函数版本,即 strcpy_s 而不是 strcpy。
回答by macbirdie
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.
所以你不会禁用所有警告,这有时可能是有害的。
回答by macbirdie
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.
我在一个多平台项目上工作,所以我不能使用 _s 函数,我不想用 Visual Studio 特定的代码污染我的代码。
我的解决方案是在 Visual Studio 项目中禁用警告 4996。转到项目 -> 属性 -> 配置属性 -> C/C++ -> 高级 -> 禁用特定警告添加值 4996。
如果您还使用 mfc 和/或 atl 库(不是我的情况)在包含 mfc _AFX_SECURE_NO_DEPRECATE 和在包含 atl _ATL_SECURE_NO_DEPRECATE 之前。
我在 Visual Studio 2003 和 2005 中使用此解决方案。
p.s. if you use only visual studio the secure template overloads could be a good solution.
ps 如果您只使用 Visual Studio,那么安全模板重载可能是一个很好的解决方案。
回答by Drealmer
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.
然后使用 _s 函数手动修复剩余的警告。
回答by PicoCreator
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)
@[macbirdie] 和 @[Adrian Borchardt] 的组合回答。事实证明这在生产环境中非常有用(不会弄乱以前存在的警告,尤其是在跨平台编译期间)
#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
回答by Gustavo Litovsky
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 */
回答by Adrian Borchardt
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
还没有让我失望;希望这可以帮助
回答by s.c
you can disable security check. go to
您可以禁用安全检查。去
Project -> Properties -> Configuration properties -> C/C++ -> Code Generation -> Security Check
项目 -> 属性 -> 配置属性 -> C/C++ -> 代码生成 -> 安全检查
and select Disable Security Check (/GS-)
并选择禁用安全检查 (/GS-)
回答by dennisV
You can define the _CRT_SECURE_NO_WARNINGS symbol to suppress them and undefine it to reinstate them back.
您可以定义 _CRT_SECURE_NO_WARNINGS 符号来抑制它们并取消定义它以恢复它们。
回答by jww
Another late answer... Here's how Microsoft uses it in their wchar.h
. Notice they also disable Warning C6386:
另一个迟到的答案......这是微软如何在他们的wchar.h
. 请注意,他们还禁用了警告 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 )
}