[英]C++17 what new with : error C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead
i try to do a simple task and get environment variable both in Linux and windows all docs point to use: std::getenv but when i run the compilation in windows 10 using visual studio 2019 I'm getting:我尝试做一个简单的任务并在 Linux 和 windows 中获取环境变量,所有文档都指向使用:std::getenv 但是当我在 Z0F4137ED1502B5045D6083AA258B5C49 中运行编译时使用:
error C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS
This is the function:这是 function:
std::string Utils::getEnvVar(std::string const& key)
{
char* val = std::getenv(key.c_str());
return val == NULL ? std::string("") : std::string(val);
}
i found only this thread , which i don't understand what is the solution for cross-platform solution.我只找到了这个线程,我不明白跨平台解决方案的解决方案是什么。
The compiler message says it all:编译器消息说明了一切:
error C4996: 'getenv': This function or variable may be unsafe.
错误 C4996:“getenv”:此 function 或变量可能不安全。 Consider using _dupenv_s instead.
考虑改用 _dupenv_s。 To disable deprecation, use _CRT_SECURE_NO_WARNINGS
要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS
The warnings is MSVC specific.警告是特定于 MSVC 的。 It warns about usage of an unsafe C standard function.
它警告使用不安全的 C 标准 function。
If you only need to compile on MSVC, then如果只需要在 MSVC 上编译,那么
Consider using _dupenv_s instead.
考虑改用 _dupenv_s。
If you need cross platform compatibility, then如果您需要跨平台兼容性,那么
To disable deprecation, use _CRT_SECURE_NO_WARNINGS
要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS
Which is a define, so just #define _CRT_SECURE_NO_WARNINGS
.这是一个定义,所以只需
#define _CRT_SECURE_NO_WARNINGS
。 Another solution would be to disable the warning ( #pragma warning(disable: 4996)
).另一种解决方案是禁用警告(
#pragma warning(disable: 4996)
)。
See also Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio .另请参阅在 Visual Studio 中默认从项目中删除安全警告 (_CRT_SECURE_NO_WARNINGS) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.