From 8d774fa48c4397cc83f06c528efa74628299cad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 28 Feb 2024 18:20:21 +0100 Subject: [PATCH] Define Windows header macros only once in `CMakeLists.txt` Instead of defining the macro `WIN32_LEAN_AND_MEAN` and sometimes also the macro `_WIN32_WINNT` in each file that directly or indirectly includes ``, only define these macros once consistently in `CMakeLists.txt`. Also define `NTDDI_VERSION`, which is the new macro to specify the minimum Windows version starting with Windows Vista. This macro needs to be defined in addition to old `_WIN32_WINNT` macro, according to the documentation. See https://learn.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers --- CMakeLists.txt | 6 ++++++ src/base/log.cpp | 3 --- src/base/system.cpp | 5 ----- src/engine/client/client.cpp | 2 -- src/engine/client/input.cpp | 1 - src/engine/server/main.cpp | 4 ---- src/engine/shared/fifo.cpp | 1 - src/engine/shared/http.cpp | 1 - 8 files changed, 6 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 883634bd5..bf541dd77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3346,7 +3346,13 @@ foreach(target ${TARGETS_OWN}) target_compile_options(${target} PRIVATE /wd4800) # Implicit conversion of int to bool. endif() if(TARGET_OS STREQUAL "windows") + # See https://learn.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers target_compile_definitions(${target} PRIVATE NOMINMAX) # windows.h shouldn't define min/max macros + target_compile_definitions(${target} PRIVATE WIN32_LEAN_AND_MEAN) # windows.h shouldn't define the name IStorage + # 0x0501 (Windows XP) is required for mingw to get getaddrinfo to work + # 0x0600 (Windows Vista) is required to use RegGetValueW and RegDeleteTreeW + target_compile_definitions(${target} PRIVATE NTDDI_VERSION=0x06000000) # Minimum OS version (new macro, since Vista) + target_compile_definitions(${target} PRIVATE _WIN32_WINNT=0x0600) # Minimum OS version (old macro, both must be defined) target_compile_definitions(${target} PRIVATE UNICODE) # Windows headers target_compile_definitions(${target} PRIVATE _UNICODE) # C-runtime endif() diff --git a/src/base/log.cpp b/src/base/log.cpp index 62e966d12..41c67c3c2 100644 --- a/src/base/log.cpp +++ b/src/base/log.cpp @@ -8,9 +8,6 @@ #include #if defined(CONF_FAMILY_WINDOWS) -#define WIN32_LEAN_AND_MEAN -#undef _WIN32_WINNT -#define _WIN32_WINNT 0x0501 /* required for mingw to get getaddrinfo to work */ #include #include #include diff --git a/src/base/system.cpp b/src/base/system.cpp index 579e3b461..b0f57c297 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -62,11 +62,6 @@ #endif #elif defined(CONF_FAMILY_WINDOWS) -#define WIN32_LEAN_AND_MEAN -#undef _WIN32_WINNT -// 0x0501 (Windows XP) is required for mingw to get getaddrinfo to work -// 0x0600 (Windows Vista) is required to use RegGetValueW and RegDeleteTreeW -#define _WIN32_WINNT 0x0600 #include #include #include diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index f19e09f4c..97001fa9e 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -1,8 +1,6 @@ /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ /* If you are missing that file, acquire a complete release at teeworlds.com. */ -#define _WIN32_WINNT 0x0501 - #include #include #include diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index 7de5a9e31..4c6ea03e9 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -25,7 +25,6 @@ #endif #if defined(CONF_FAMILY_WINDOWS) -#define WIN32_LEAN_AND_MEAN #include // windows.h must be included before imm.h, but clang-format requires includes to be sorted alphabetically, hence this comment. #include diff --git a/src/engine/server/main.cpp b/src/engine/server/main.cpp index ebe978a3c..5b7e0a4ae 100644 --- a/src/engine/server/main.cpp +++ b/src/engine/server/main.cpp @@ -1,6 +1,3 @@ - -#define _WIN32_WINNT 0x0501 - #include #include @@ -23,7 +20,6 @@ #include #if defined(CONF_FAMILY_WINDOWS) -#define WIN32_LEAN_AND_MEAN #include #endif diff --git a/src/engine/shared/fifo.cpp b/src/engine/shared/fifo.cpp index af2b49f09..9af990a99 100644 --- a/src/engine/shared/fifo.cpp +++ b/src/engine/shared/fifo.cpp @@ -79,7 +79,6 @@ void CFifo::Update() #elif defined(CONF_FAMILY_WINDOWS) -#define WIN32_LEAN_AND_MEAN #include void CFifo::Init(IConsole *pConsole, char *pFifoFile, int Flag) diff --git a/src/engine/shared/http.cpp b/src/engine/shared/http.cpp index 225a60ffc..695fc2deb 100644 --- a/src/engine/shared/http.cpp +++ b/src/engine/shared/http.cpp @@ -15,7 +15,6 @@ #include #endif -#define WIN32_LEAN_AND_MEAN #include // There is a stray constant on Windows/MSVC...