From da548b83e798650e1e07c3cd8f042737cdde0124 Mon Sep 17 00:00:00 2001 From: Sharadh Rajaraman Date: Mon, 28 Sep 2026 16:19:45 +0100 Subject: [PATCH] Guard system includes for inclusion in a C++20 module purview imgui.cppm compiles the core sources inside its module purview. System headers #included there would attach their declarations to the module, which is ill-formed and clashes with the same declarations in the global module or in 'import std;'. The module includes them in its global module fragment instead, so under IMGUI_CXX_MODULE the sources skip their own includes. Header builds are unchanged. For the same reason: - imstb_rectpack.h and imstb_truetype.h skip their extern "C" blocks, which would attach their declarations to the global module. - imgui_draw.cpp defines STBTT_cos/acos/memcpy/memset, so stb_truetype doesn't include and itself. --- imgui.cpp | 16 +++++++++++++++- imgui.h | 2 ++ imgui_demo.cpp | 2 ++ imgui_draw.cpp | 6 ++++++ imgui_internal.h | 6 ++++++ imgui_tables.cpp | 2 ++ imgui_widgets.cpp | 2 ++ imstb_rectpack.h | 4 ++-- imstb_truetype.h | 4 ++-- 9 files changed, 39 insertions(+), 5 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index fb86370b9..ae6d48f16 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1257,10 +1257,14 @@ IMPLEMENTING SUPPORT for ImGuiBackendFlags_RendererHasTextures: #include "imgui_internal.h" // System includes +#ifndef IMGUI_CXX_MODULE #include // vsnprintf, sscanf, printf #include // intptr_t +#endif #ifndef IMGUI_DISABLE_TIME_FUNCTIONS +#ifndef IMGUI_CXX_MODULE #include // time(), localtime_r()/localtime_s() +#endif #if defined(_WIN32) static tm* localtime_r(const time_t* timep, tm* result) { return localtime_s(result, timep) == 0 ? result : NULL; } #endif @@ -1282,11 +1286,13 @@ static tm* localtime_r(const time_t* timep, tm* result) { return localtime_s(res #ifndef NOMINMAX #define NOMINMAX #endif +#ifndef IMGUI_CXX_MODULE #ifndef __MINGW32__ #include // _wfopen, OpenClipboard #else #include #endif +#endif #if defined(WINAPI_FAMILY) && ((defined(WINAPI_FAMILY_APP) && WINAPI_FAMILY == WINAPI_FAMILY_APP) || (defined(WINAPI_FAMILY_GAMES) && WINAPI_FAMILY == WINAPI_FAMILY_GAMES)) // The UWP and GDK Win32 API subsets don't support clipboard nor IME functions #define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS @@ -1296,7 +1302,7 @@ static tm* localtime_r(const time_t* timep, tm* result) { return localtime_s(res #endif // [Apple] OS specific includes -#if defined(__APPLE__) +#if defined(__APPLE__) && !defined(IMGUI_CXX_MODULE) #include #endif @@ -16282,7 +16288,9 @@ static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext*, const char* t #elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX && defined(IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS) +#ifndef IMGUI_CXX_MODULE #include // Use old API to avoid need for separate .mm file +#endif static PasteboardRef main_clipboard = 0; // OSX clipboard implementation @@ -16370,7 +16378,9 @@ static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext* ctx, const cha #ifndef IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS #ifdef _WIN32 +#ifndef IMGUI_CXX_MODULE #include // ShellExecuteA() +#endif #ifdef _MSC_VER #pragma comment(lib, "shell32") #endif @@ -16383,8 +16393,10 @@ static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) return (INT_PTR)::ShellExecuteW(NULL, L"open", path_wbuf.Data, NULL, NULL, SW_SHOWDEFAULT) > 32; } #else +#ifndef IMGUI_CXX_MODULE #include #include +#endif static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) { #if defined(__APPLE__) @@ -16417,7 +16429,9 @@ static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char*) { ret // Win32 API IME support (for Asian languages, etc.) #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) +#ifndef IMGUI_CXX_MODULE #include +#endif #ifdef _MSC_VER #pragma comment(lib, "imm32") #endif diff --git a/imgui.h b/imgui.h index 7b7cccd9b..7f0b2915e 100644 --- a/imgui.h +++ b/imgui.h @@ -64,10 +64,12 @@ Index of this file: //----------------------------------------------------------------------------- // Includes +#ifndef IMGUI_CXX_MODULE #include // FLT_MIN, FLT_MAX #include // va_list, va_start, va_end #include // ptrdiff_t, NULL #include // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp +#endif // Warnings #ifdef _MSC_VER diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 0b16dab3c..eae093265 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -135,6 +135,7 @@ Index of this file: #ifndef IMGUI_DISABLE // System includes +#ifndef IMGUI_CXX_MODULE #include // toupper #include // INT_MIN, INT_MAX #include // sqrtf, powf, cosf, sinf, floorf, ceilf @@ -147,6 +148,7 @@ Index of this file: #ifdef __EMSCRIPTEN__ #include // __EMSCRIPTEN_MAJOR__ etc. #endif +#endif // Visual Studio warnings #ifdef _MSC_VER diff --git a/imgui_draw.cpp b/imgui_draw.cpp index a4097bf38..9f3eb5daa 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -40,8 +40,10 @@ Index of this file: #include "misc/freetype/imgui_freetype.h" #endif +#ifndef IMGUI_CXX_MODULE #include // vsnprintf, sscanf, printf #include // intptr_t +#endif // Visual Studio warnings #ifdef _MSC_VER @@ -150,6 +152,10 @@ namespace IMGUI_STB_NAMESPACE #define STBTT_ifloor(x) ((int)ImFloor(x)) #define STBTT_iceil(x) ((int)ImCeil(x)) #define STBTT_strlen(x) ImStrlen(x) +#define STBTT_cos(x) cos(x) // Same as stb_truetype defaults, but avoid it including / itself (e.g. in C++20 module purview) +#define STBTT_acos(x) acos(x) +#define STBTT_memcpy memcpy +#define STBTT_memset memset #define STBTT_STATIC #define STB_TRUETYPE_IMPLEMENTATION #else diff --git a/imgui_internal.h b/imgui_internal.h index 1bdbf1733..c55eaaff7 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -52,20 +52,26 @@ Index of this file: #include "imgui.h" +#ifndef IMGUI_CXX_MODULE #include // FILE*, sscanf #include // NULL, malloc, free, qsort, atoi, atof #include // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf #include // INT_MIN, INT_MAX +#endif // Enable SSE intrinsics if available #if (defined __SSE__ || defined __x86_64__ || defined _M_X64 || (defined(_M_IX86_FP) && (_M_IX86_FP >= 1))) && !defined(IMGUI_DISABLE_SSE) && !defined(_M_ARM64) && !defined(_M_ARM64EC) #define IMGUI_ENABLE_SSE +#ifndef IMGUI_CXX_MODULE #include +#endif #if (defined __AVX__ || defined __SSE4_2__) #define IMGUI_ENABLE_SSE4_2 +#ifndef IMGUI_CXX_MODULE #include #endif #endif +#endif // Emscripten has partial SSE 4.2 support where _mm_crc32_u32 is not available. See https://emscripten.org/docs/porting/simd.html#id11 and #8213 #if defined(IMGUI_ENABLE_SSE4_2) && !defined(IMGUI_USE_LEGACY_CRC32_ADLER) && !defined(__EMSCRIPTEN__) #define IMGUI_ENABLE_SSE4_2_CRC diff --git a/imgui_tables.cpp b/imgui_tables.cpp index b3e0458eb..150f5c83e 100644 --- a/imgui_tables.cpp +++ b/imgui_tables.cpp @@ -202,7 +202,9 @@ Index of this file: #include "imgui_internal.h" // System includes +#ifndef IMGUI_CXX_MODULE #include // intptr_t +#endif // Visual Studio warnings #ifdef _MSC_VER diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index ceabc13ba..5f2b361a4 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -45,7 +45,9 @@ Index of this file: #include "imgui_internal.h" // System includes +#ifndef IMGUI_CXX_MODULE #include // intptr_t +#endif //------------------------------------------------------------------------- // Warnings diff --git a/imstb_rectpack.h b/imstb_rectpack.h index ad0892130..ffaab7f62 100644 --- a/imstb_rectpack.h +++ b/imstb_rectpack.h @@ -78,7 +78,7 @@ #define STBRP_DEF extern #endif -#ifdef __cplusplus +#if defined(__cplusplus) && !defined(IMGUI_CXX_MODULE) // [DEAR IMGUI] extern "C" would attach declarations to the global module extern "C" { #endif @@ -195,7 +195,7 @@ struct stbrp_context stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' }; -#ifdef __cplusplus +#if defined(__cplusplus) && !defined(IMGUI_CXX_MODULE) // [DEAR IMGUI] } #endif diff --git a/imstb_truetype.h b/imstb_truetype.h index ec4d42c7d..87f6c9af8 100644 --- a/imstb_truetype.h +++ b/imstb_truetype.h @@ -510,7 +510,7 @@ int main(int arg, char **argv) #define STBTT_DEF extern #endif -#ifdef __cplusplus +#if defined(__cplusplus) && !defined(IMGUI_CXX_MODULE) // [DEAR IMGUI] extern "C" would attach declarations to the global module extern "C" { #endif @@ -1095,7 +1095,7 @@ enum { // languageID for STBTT_PLATFORM_ID_MAC STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19 }; -#ifdef __cplusplus +#if defined(__cplusplus) && !defined(IMGUI_CXX_MODULE) // [DEAR IMGUI] } #endif