简体   繁体   English

C++17 什么新:错误 C4996:'getenv':此 function 或变量可能不安全。 考虑改用 _dupenv_s

[英]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.

相关问题 错误C4996:'ctime':此函数或变量可能不安全 - error C4996: 'ctime': This function or variable may be unsafe Tesseract baseapi错误:'strncpy':此函数或变量可能不安全。 考虑改用strncpy_s - Tesseract baseapi error: 'strncpy':this function or variable may be unsafe. Consider using strncpy_s instead sprintf错误“变量可能不安全”(C4996)…替代品? - c++ - sprintf error “variable may be unsafe” (C4996)… alternatives? 错误C4996:'std :: _ Copy_impl':带有可能不安全的参数的函数调用 - error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe Visual Studio 2015错误C4996'std :: _ Copy_impl':使用参数可能不安全的函数调用 - Visual Studio 2015 error C4996 'std::_Copy_impl': Function call with parameters that may be unsafe 警告C4996:与POSIX上的GCC相比,此功能或变量可能不安全 - Warning C4996: This function or variable may be unsafe — compared to GCC on POSIX 使用升压示例时出现错误C4996 - Error C4996 on using boost example 使用C ++ strtk导致xutility错误C4996 - Using C++ strtk results in an xutility error C4996 带有_wfopen函数的C ++ C4996错误 - C++ C4996 error with _wfopen function OpenCL编译器错误C4996 - OpenCL Compiler Error C4996
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM