包括stdio在调用WriteConsole时导致错误

rsk*_*k82 3 c++ console winapi mingw

#include <stdio.h>
#include <windows.h>

using namespace std;

int main() {
  char s[] = "Hello\n";
  HANDLE stdout = GetStdHandle(STD_OUTPUT_HANDLE);
  unsigned long cChars;
  WriteConsole(stdout, s, lstrlen(s), &cChars, NULL);
}
Run Code Online (Sandbox Code Playgroud)

结果: error: declaration of '_iob' as array of references

但是当我评论出来时stdio.h,它编译好了.这有什么不对?

编译器是MinGW.

Pat*_*Pat 7

根据平台,stdout可能是一个宏,所以最好不要使用该名称.更换

HANDLE stdout = ...
Run Code Online (Sandbox Code Playgroud)

HANDLE out = ...
Run Code Online (Sandbox Code Playgroud)

然后

WriteConsole(out, ...
Run Code Online (Sandbox Code Playgroud)

stdout在stdio.h中定义,这就是为什么它只在包含这个文件时才会失败.但为了安全起见,永远不要将该名称用于任何自定义变量.