From bb5404bb86160ac0c1e56d852ba345c48dbd7f33 Mon Sep 17 00:00:00 2001 From: Hariom Phulre Date: Fri, 15 May 2026 20:34:42 +0530 Subject: [PATCH] Fix GCC C++20 modules compilation #5103 (#5164) * Fix: Add GCC diagnostic pragmas for C++ modules support (issue #5103) Signed-off-by: hariomphulre * Fix: GCC C++20 modules with __cplusplus >= 202002L check (#5103) Signed-off-by: hariomphulre Signed-off-by: hariomphulre * Fix: Remove indentation from nested preprocessor directives and disable astyle preprocessor indentation Signed-off-by: hariomphulre * Update amalgamated files with correct preprocessor directive indentation Signed-off-by: hariomphulre * Fix GCC build for issue #5103; refresh amalgamated header Signed-off-by: hariomphulre --------- Signed-off-by: hariomphulre --- include/nlohmann/json.hpp | 15 +++++++++++++++ single_include/nlohmann/json.hpp | 15 +++++++++++++++ src/modules/json.cppm | 16 +++++++++------- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index f5826191a..47d5c51ff 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -18,6 +18,16 @@ #ifndef INCLUDE_NLOHMANN_JSON_HPP_ #define INCLUDE_NLOHMANN_JSON_HPP_ +// Workaround for GCC template redefinition errors in C++ modules +// When nlohmann/json.hpp is included in a C++20 module preamble after +// other module imports, GCC may report spurious redefinition errors for +// STL templates. These pragmas suppress those false positives. +// See: https://github.com/nlohmann/json/issues/5103 +#if defined(__GNUC__) && !defined(__clang__) && __cplusplus >= 202002L + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wignored-attributes" +#endif + #include // all_of, find, for_each #include // nullptr_t, ptrdiff_t, size_t #include // hash, less @@ -5344,4 +5354,9 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC #include +// End of GCC diagnostic pragmas for C++ modules support +#if defined(__GNUC__) && !defined(__clang__) && __cplusplus >= 202002L + #pragma GCC diagnostic pop +#endif + #endif // INCLUDE_NLOHMANN_JSON_HPP_ diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 65b3b3459..e3e701557 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18,6 +18,16 @@ #ifndef INCLUDE_NLOHMANN_JSON_HPP_ #define INCLUDE_NLOHMANN_JSON_HPP_ +// Workaround for GCC template redefinition errors in C++ modules +// When nlohmann/json.hpp is included in a C++20 module preamble after +// other module imports, GCC may report spurious redefinition errors for +// STL templates. These pragmas suppress those false positives. +// See: https://github.com/nlohmann/json/issues/5103 +#if defined(__GNUC__) && !defined(__clang__) && __cplusplus >= 202002L + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wignored-attributes" +#endif + #include // all_of, find, for_each #include // nullptr_t, ptrdiff_t, size_t #include // hash, less @@ -25859,4 +25869,9 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC +// End of GCC diagnostic pragmas for C++ modules support +#if defined(__GNUC__) && !defined(__clang__) && __cplusplus >= 202002L + #pragma GCC diagnostic pop +#endif + #endif // INCLUDE_NLOHMANN_JSON_HPP_ diff --git a/src/modules/json.cppm b/src/modules/json.cppm index 0930483af..15207535f 100644 --- a/src/modules/json.cppm +++ b/src/modules/json.cppm @@ -8,6 +8,15 @@ module; +// GCC workaround for C++ modules support. +// When using C++20 modules, some compilers (particularly GCC) may have issues +// with template instantiations in the module preamble. If you encounter +// "redefinition" errors when including nlohmann/json.hpp, try one of: +// 1. Include nlohmann/json.hpp in your module preamble BEFORE other #includes +// 2. Or use: import nlohmann.json; instead of #include +// 3. Or upgrade to a newer GCC version with better modules support. +// See: https://github.com/nlohmann/json/issues/5103 + #include export module nlohmann.json; @@ -41,10 +50,3 @@ namespace detail } // namespace detail NLOHMANN_JSON_NAMESPACE_END - -export namespace std -{ - using std::hash; - using std::less; - using std::swap; -} // namespace std