<iostream> 和 #define __STDC_WANT_SECURE_LIB__ 0 导致错误 C2039:'sprintf_s'

jes*_*ses 7 visual-c++ secure-scl visual-studio-2019

在构建这个非常简单的测试程序时

#include <iostream>
int main() {
    std::cout << "x";
}
Run Code Online (Sandbox Code Playgroud)

使用 Visual Studio 2019,/Wall我得到了

warning C4668: '__STDC_WANT_SECURE_LIB__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
Run Code Online (Sandbox Code Playgroud)

尝试去

#define __STDC_WANT_SECURE_LIB__ 0
Run Code Online (Sandbox Code Playgroud)

在包含 iostream 结果之前

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xlocnum(1388,69): error C2039: 'sprintf_s': is not a member of '`global namespace''
Run Code Online (Sandbox Code Playgroud)

至少对于我的VS来说是这样。戈德博尔特没有抱怨

#define __STDC_WANT_SECURE_LIB__ 1
Run Code Online (Sandbox Code Playgroud)

很好,不会让编译器抱怨人们所期望的 sprintf_s 。

搜索时微软没有向我显示任何结果。这里也是如此,但总的来说,我找不到很多关于是否以及如何使用该定义的资源。

有没有办法禁用安全扩展并包含<iostream>?我是否使用了错误的定义或方法?

小智 0

我有同样的错误,不需要许可......等等等等

VS Community 2022 中的“ STDC_WANT_SECURE_LIB ”,带有预编译的 CMake SFML-2.6.x(!)

我的错误是将错误的路径放入文件夹"SFML-2.6.x\SFML_cmake_x64\lib\Debug" - 我没有包含文件夹"\Debug"。结果是:“SFML-2.6.x\SFML_cmake_x64\lib” ——这是错误的方式!!!!

每次您都必须使用\Debug\Release版本将完整路径放入文件夹“lib”