审查下个季度优化的 C++ 类(class)的 Material 。本类(class)的教授正在为我们的项目属性强制执行/WALL 和/WX。我遇到的问题是,包括 Iostream 库会产生 800 多个警告。这是我尝试运行的代码:
#include "pch.h"
#include <iostream>
int main() {
std::cout << "Hello World";
return(0);
}
我收到的一些警告包括:
C4514 'abs': unreferenced inline function has been removed
C4774 'sprintf_s': format string in argument 3 is not a string literal
C4820 'std::basic_ios ...': '7' bytes of padding added after...
在询问 Stack 之前,我给教授发了电子邮件询问警告,并被告知:
You should be including iostream
If you get 100 warnings you included a header that's not needed
有什么我想念的吗?我知道我无法编辑 iostream 的源文件,因为那不是可移植的编码。我环顾四周,看看是否可以明确包含 cout、cin 等函数。但是,我觉得这不是正确的解决方案。
编辑:
用户要求提供更明确的警告消息示例,以防其中缺少某些内容。这里有一些:
C415 'abs': referenced inline function has been removed (Project: Hello World) (File: stdlib.h)
C4710 'int sprintf_s(char *const....: function not inlined. (Project: Hello World) (File: stdio.h)
教授通过 Visual Studio 使用 GCC,我们的设置作为预制项目从存储库中提取。
最佳答案
很简单,您的教授是错误的。
这与“包含不需要的 header ”无关(为什么会产生警告?),但与使用 /WALL
有关, which reveals some flaws in the stdlib implementation there !
不推荐使用此开关;引用 James McNellis谁gets it bang-on在上述问题下:
/Wall
enables many warnings that, while potentially useful sometimes, are not useful most of the time./Wall
in Visual C++ does not mean the same thing as-Wall
on g++ (really, g++ "has/Wall
wrong," since it doesn't actually enable all warnings). In any case, in Visual C++, all of the commonly important and useful warnings are enabled by/W4
.
我会使用 /W4
在 Visual Studio 中(在 GCC 中为 -Wall -Wextra
)。
显然我无法帮助你说服你的教授,除了建议说一些类似的东西“我在 Stack Overflow 上问过,发现这是由于 /Wall
太严格了并在 Visual Studio 自己的 header 上生成警告。他们建议我们改用 /W4
。你怎么看?”
确实需要#include <iostream>
, 的确,您永远不应该修改提供的标准 header 。另外,不要忘记流式传输 '\n'
结束你的输出行!
关于c++ - (MSVC 2017/WALL &/WX) 包括 Iostream 产生 800 个警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54161129/