Fix Windows header include order#77
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Header files should not depend on the order that they are included. Specifically, header files should not depend on other header files being included first. Unfortunately, on Windows, the order that Windows system header files are included is important. For example,
winsock2.hwill raise a warning if it is not included beforewindows.h:The problem is, even if we include
winsock2.hbeforewindows.hin the header file wherewinsock2.his included, it doesn't guarantee thatwindows.hhas not been included by another header file earlier. The solution is not to includewinsock2.hbefore every time we includewindows.h, becausewinsock2.hmay not be needed there. Furthermore, this problem is not limited towinsock2.h. For example, in e557849,mmsystem.hneeded to be included afterwindows.h.The solution requires understanding why
winsock2.hneeds to be included beforewindows.h, andmmsystem.hneeds to be included afterwindows.h.windows.his often included when developing on Windows, because it defines many macros that Windows code uses. Unfortunately, presumably to make it simpler for beginner developers,windows.halso includes a number of other Windows system header files; even if they are not required or wanted. One of these system header files iswinsock.hand another ismmsystem.h.winsock2.hwants to be included beforewinsock.h.mmsystem.honly wants to be included if it's not included withwindows.h.To avoid,
winsock.h,mmsystem.hand other unnecessary Windows system headers being included wheneverwindows.his included,WIN32_LEAN_AND_MEANcan be defined beforewindows.his included for the first time. To ensureWIN32_LEAN_AND_MEANis defined the first timewindows.his included, it must be defined before every timewindows.his included. Unfortunately, since many other Windows system header files may also includewindows.h, we need to defineWIN32_LEAN_AND_MEANbefore every Windows system header file is included. Furthermore, since Rebel's coding style (#19) ensures that header files are included in alphabetical order, which would placewindows.hbelow most other system header files, we need to ensure that enforcing Rebel's code style does not place other Windows system header files before#define WIN32_LEAN_AND_MEANand#include <windows.h>.This PR ensures that:
#define WIN32_LEAN_AND_MEANis placed before every#include <windows.h>#define WIN32_LEAN_AND_MEANand#include <windows.h>are placed before other Windows system files// Windows system includes come after <windows.h>) is placed before other Windows system files to break the include block and the alphabetic header file ordering, and explains the deliberate change from alphabetic ordering of includes.