commit e986c2bdfcc5b2d9b9af6dc8d8cf98b26bf8bf95
parent ad5c51354b622cf6494f4bd04b6952b1540dc9b7
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Sat, 29 Aug 2026 14:16:52 +0200
Improved compatibility with C++ compilers
Diffstat:
2 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/configure.ac b/configure.ac
@@ -2105,7 +2105,42 @@ AC_CACHE_CHECK([[for C11 'alignof()' support]], [[mhd_cv_c_alignof]],
], [[mhd_cv_c_alignof='no']])
])
AS_VAR_IF([mhd_cv_c_alignof], ["yes"],
- [AC_DEFINE([[HAVE_C_ALIGNOF]], [1], [Define to 1 if your compiler supports 'alignof()'])])
+ [AC_DEFINE([[HAVE_C_ALIGNOF]], [1], [Define to 1 if your compiler supports 'alignof()'])]
+ ,
+ [
+ AC_CACHE_CHECK([[whether $CC supports __alignof__() as compile-time constant]], [[mhd_cv_cc___alignof]],
+ [
+ AC_LINK_IFELSE(
+ [
+ AC_LANG_SOURCE(
+ [[
+
+int main(void)
+{
+ int a = __alignof__(int);
+ switch(a)
+ {
+ case 0:
+ break;
+ case __alignof__(a):
+ a = 0;
+ break;
+ default:
+ break;
+ }
+ return a;
+}
+ ]]
+ )
+ ],
+ [mhd_cv_cc___alignof="yes"],[mhd_cv_cc___alignof="no"]
+ )
+ ]
+ )
+ AS_VAR_IF([mhd_cv_cc___alignof], ["yes"],
+ [AC_DEFINE([[HAVE___ALIGNOF]], [1], [Define to 1 if your compiler supports '__alignof__()'])])
+ ]
+)
AC_CACHE_CHECK([whether $CC supports __attribute__((aligned ( )))],[mhd_cv_cc_attr_aligned],
[
diff --git a/src/mhd2/mhd_align.h b/src/mhd2/mhd_align.h
@@ -85,6 +85,14 @@
# endif
#endif
+#if !defined(mhd_ALIGNOF) && defined(__cplusplus)
+/* __alignof__ is preferred alignment, not minimal required alignment,
+ but offsetof() trick does not work in C++, so it is the best available option */
+# ifdef HAVE___ALIGNOF
+# define mhd_ALIGNOF(TYPE) __alignof__(TYPE)
+# endif
+#endif /* !mhd_ALIGNOF && __cplusplus */
+
#ifndef mhd_ALIGNOF
# include "sys_offsetof.h"
# define mhd_ALIGNOF(TYPE) \