Why does changing the order of including psapi.h gives compilation erros?(Indentifier BOOL is undefined)

折月煮酒 提交于 2021-02-11 12:47:14

问题


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();
}

But if I change the order of #includes 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?


回答1:


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 is defined in minwindef.h (#157: typedef int BOOL;)

  • Psapi.h only includes one file (#27: #include <winapifamily.h>)

    • winapifamily.h doesn't include any other file

So, when reaching Psapi.h (#87: BOOL WINAPI EnumProcesses (...), the compiler doesn't know anything about BOOL, so it complains.

Windows.h includes minwindef.h (indirectly, via windef.h), and that's why it works when you include it before Psapi.h.

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 1st one :)

#include <Windows.h>
#include <WinSock2.h>

// main present just for rigorosity's sake
int main() {
    return 0;
}


来源:https://stackoverflow.com/questions/52367594/why-does-changing-the-order-of-including-psapi-h-gives-compilation-errosindent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!