问题描述
我正在使用 Visual Studio Community 2017 编写 C++.当我运行以下代码时,一切正常.
I am using Visual Studio Community 2017 to code c++. When I run the following code everything works fine.
#include "pch.h"
#include<Windows.h>
#include<Psapi.h>
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Really!! How do you do it?";
_getch();
}
但是如果我通过在 Windows.h
之前包含 psapi.h
来更改 #include
的顺序,编译器会变得很糟糕并抛出 198我的错误,令人惊讶的是(也许只有我自己)包括标识符BOOL"未定义.为什么会发生这种情况?
But if I change the order of #include
s by including psapi.h
before Windows.h
, compiler goes badass and throws 198 errors at me, which surprisingly(maybe only to me) includes Identifier "BOOL" is undefined.Why is this happening?
推荐答案
由于 Psapi.h 的包含树是微不足道的,我将举例说明.
一切都依赖于 VStudio 2015(社区)(v14.0.25431.01 更新 3)和 Windows 工具包8.1(?有趣,因为v10也有)文件(带有默认的环境变量和预处理器定义):
Since Psapi.h's include tree is trivial, I'm going to exemplify.
Everything relies on VStudio 2015 (Community) (v14.0.25431.01 Update 3) and Windows Kits 8.1 (? funny, because v10 is there too) files (with default env vars and preprocessor definitions):
BOOL 在 minwindef.h 中定义(#157:
typedef intBOOL;
)
Psapi.h 仅包含一个文件(#27:#include
)
- winapifamily.h 不包含任何其他文件
- winapifamily.h doesn't include any other file
所以,当到达 Psapi.h (#87: BOOL WINAPI EnumProcesses (...
),编译器对 BOOL 一无所知,所以它会抱怨.
So, when reaching Psapi.h (#87: BOOL WINAPI EnumProcesses (...
), the compiler doesn't know anything about BOOL, so it complains.
Windows.h 包括 minwindef.h(间接地,通过windef.h),这就是为什么当您在 Psapi.h 之前包含它时,它会起作用.
Windows.h includes minwindef.h (indirectly, via windef.h), and that's why it works when you include it before Psapi.h.
就个人而言,我认为这是 Psapi.h 中的一个错误,因为它不是自包含,但可能有一个很好的理由(我不知道) 为了那个原因.
无论如何,如果这确实是一个错误,它就不会是MS的第一个 :)
Personally, I think it's a bug in Psapi.h, since it's not self contained, but there might be a good reason (that I'm not aware of) for that.
Anyway, if this is indeed a bug, it wouldn't be MS's 1 one :)
#include <Windows.h>
#include <WinSock2.h>
// main present just for rigorosity's sake
int main() {
return 0;
}
这篇关于为什么更改包含 psapi.h 的顺序会导致编译错误?(Indentifier BOOL 未定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!