c++ - 从包含打破其他先前包含定义

标签 c++ c-preprocessor

我正在尝试调试项目主文件的包含。这是我的包含代码。

//Gameplay
#include "gameplay.h"

//LibNoise
#include <noise/noise.h>

//Console Window
#ifndef _WINDOWS_
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #undef KEY_EVENT
    #undef MOUSE_EVENT
#endif
#include <io.h>
#include <fcntl.h>

//RakNet
#include "MessageIdentifiers.h"
#include "RakPeerInterface.h"
#include "BitStream.h"
#include "RakNetTypes.h"

//My Includes
#include "Island.h"

问题是,gameplay.h 包含一个文件(特别是 ScriptController.h),其中有一个包含单词 KEY_EVENT 和 MOUSE_EVENT 的枚举,它们通过某些 include 包含在 windows.h(特别是 wincon.h)中。这破坏了枚举,我在编译过程中遇到错误。请注意,它实际上包括 windows.h,因为 _WINDOWS_ 此时未根据 MSVS 定义(因此它不像是在 gameplay.h 或其他内容之前定义的)。

我不明白为什么这会成为一个问题,因为 gameplay.h 包含在 windows.h 之前,这应该意味着我可以轻松替换枚举中的条款?取消定义它们也无济于事。

我哪里错了?有什么办法可以“调试”预处理器并查看导致此语法错误和某种#include 链的预处理器输出?如果下次发生这种情况,我希望能够自己解决。

这里是错误

Error 3 error C2065: 'CALLBACK_COUNT' : undeclared identifier c:\users\pf\downloads\gameplay-master\gameplay\src\scriptcontroller.h 1024 1 testerino2
Error 1 error C2059: syntax error : 'constant' c:\users\pf\downloads\gameplay- master\gameplay\src\scriptcontroller.h 769 1 testerino2
Error 2 error C3805: 'constant': unexpected token, expected either '}' or a ',' c:\users\pf\downloads\gameplay-master\gameplay\src\scriptcontroller.h 769 1 testerino2

这是 wincon.h 定义

#define KEY_EVENT         0x0001 // Event contains key event record
#define MOUSE_EVENT       0x0002 // Event contains mouse event record

这是 ScriptController.h 的违规代码行

762| enum ScriptCallback
763| {
764|     INITIALIZE = 0,
    ...
768|     RESIZE_EVENT,
769|     KEY_EVENT,
770|     MOUSE_EVENT,
771|     TOUCH_EVENT,
    ...
775|     GAMEPAD_EVENT,
776|     CALLBACK_COUNT,
777|     INVALID_CALLBACK = CALLBACK_COUNT
778| };

...

1024| std::vector<std::string> _callbacks[CALLBACK_COUNT];

最佳答案

避免与windows.h发生冲突定义,我建议您制作自己的头文件:

  1. 定义任何影响 windows.h 的宏,例如 WIN32_LEAN_AND_MEAN
  2. 是否#include <winsock2.h>如果使用,和 #include <windows.h>
  3. 是否#undef在任何烦人的宏上

然后确保可能遇到 Windows include 的每个源文件都包含此 header 。您也许可以使用编译器功能将 header 注入(inject)所有单元。

关于c++ - 从包含打破其他先前包含定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25559722/

相关文章:

c++ - 小数点不显示

C++(Qt5.5)通过保存功能扩展图片查看器实例

c++ - 使用 Clang 和 LLVM

c++ - 尝试在另一个类中使用类时出错

c++ - 预处理器宏扩展到另一个预处理器指令

c++ - 带有尾随注释的多行预处理器宏

c++ - 使用在 Google Test 下测试的现有 C++ 代码并添加 Qt

c - 错误 C2003 : expected 'defined id'

c - 当预处理器处理预处理器行时会发生什么? - '.i' 文件

objective-c - xcode 中的 strcpy