C++ 如何防止宏重定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3927810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:04:42  来源:igfitidea点击:

How to prevent macro redefinition

c++visual-studiomacros

提问by Killrazor

After working some time on my project, this warning begins to appear:

在我的项目上工作一段时间后,此警告开始出现:

2>Game.cpp
2>c:\program files\microsoft sdks\windows\v6.0a\include\windef.h(126) : warning C4005: 'APIENTRY' : redefinición de macro
2>        c:\users\ferran\directo\gameprojects\dev-libs\glfw\include\glfw.h(72) : vea la definición anterior de 'APIENTRY'
2>c:\program files\microsoft sdks\windows\v6.0a\include\wingdi.h(23) : warning C4005: 'WINGDIAPI' : redefinición de macro
2>        c:\users\ferran\directo\gameprojects\dev-libs\glfw\include\glfw.h(88) : vea la definición anterior de 'WINGDIAPI'

I'm sure that it's a matter of the order of the include files to solve, because none of these files are mine. My question is if there is a generic way to prevent this or to find which files must to be reordered to avoid this message.

我确信这是包含文件顺序的问题,因为这些文件都不是我的。我的问题是,是否有一种通用方法可以防止这种情况发生,或者找到必须重新排序哪些文件才能避免出现此消息。

回答by Mark Ransom

The error message itself is telling you the incorrect order. It says that windef.hand wingdi.hare redefining symbols that were defined in glfw.h.

错误消息本身告诉您不正确的顺序。它说,windef.h并且wingdi.h正在重新定义在glfw.h.

Put glfw.hafter the Windows include files.

放在glfw.hWindows 包含文件之后。

回答by Cheers and hth. - Alf

Microsoft doesn't generally design headers to be free-standing. Most of the Windows-oriented headers require that you have first included <windows.h>. Except for the dependency on that Mother Of All Headers, usually there are no specific header dependencies so by including <windows.h>first you shouldn't have any problem.

Microsoft 通常不会将标题设计为独立的。大多数面向 Windows 的头文件要求您首先包含<windows.h>. 除了对所有标头之母的依赖之外,通常没有特定的标头依赖项,因此<windows.h>首先包含您应该不会有任何问题。

回答by tibur

The problem is in the file Game.cpp. Try to include windows.hbefore glfw.h. There is a guard in glfw.hwhich will prevent that warning:

问题出在文件Game.cpp 中。尝试在glfw.h之前包含windows.hglfw.h 中有一个守卫可以防止该警告:

#ifndef APIENTRY
 #ifdef _WIN32
  #define APIENTRY __stdcall
 #else
  #define APIENTRY
 #endif
 #define GL_APIENTRY_DEFINED
#endif // APIENTRY

回答by tibur

Unfortunately or fortunately, no. There is no such tool that automates it. You have to go read the code in those header files, figure out what is going on and take appropriate actions.

不幸或幸运的是,没有。没有这样的工具可以自动化它。您必须阅读这些头文件中的代码,弄清楚发生了什么并采取适当的措施。

The most you can do is

你能做的最多的是

  1. Check if macro is defined using ifdefor if defined(...)or if !defined(...)preprocessor constructs.
  2. Undefine macro using undef.
  1. 检查宏是否使用ifdefif defined(...)if !defined(...)预处理器构造定义。
  2. 使用undef.

Only ANSI C considers macro redefinition an error.

只有 ANSI C 将宏重定义视为错误。

回答by verisimilidude

This may be caused by Visual Studio pre-compiling headers for you. Be sure that all standard and microsoft headers are included before yours. Don't include microsoft headers in any of your .h files (it looks like you have windef.h and wingdi.h inlcuded in your glfw.h). Be sure that all your headers are side-effect free. The problem should then go away. Figuring out exactly what is causing it is generally very hard.

这可能是由 Visual Studio 为您预编译标头引起的。确保所有标准和微软标头都包含在您的标头之前。不要在您的任何 .h 文件中包含 microsoft 头文件(看起来您的 glfw.h 中包含了 windef.h 和 wingdi.h)。确保您的所有标题都没有副作用。然后问题应该消失了。弄清楚究竟是什么导致了它通常是非常困难的。