>       mbsstr, mbscasestr, mbspcasecmp: Use const-improved C++ templates.
>       * lib/string.in.h (mbsstr, mbspcasecmp, mbscasestr): In C++, define
>       through a template that supports both 'char *' and 'const char *'.

On AIX 7.1 with xlc I get these compilation errors:

xlC -q64 -qthreaded -qtls -DHAVE_CONFIG_H -DEXEEXT=\"\" -DEXEEXT=\"\" -I. 
-I../../gltests -I..  -DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. 
-I../../gltests -I.. -I../../gltests/.. -I../gllib -I../../gltests/../gllib 
-I/home/haible/prefix64/include -D_THREAD_SAFE  -g -c -o test-strings-h-c++.o 
../../gltests/test-strings-h-c++.cc
"../gllib/string.h", line 1723.7: 1540-0121 (S) A template cannot have "C" 
linkage.
"../gllib/string.h", line 1725.17: 1540-0603 (S) The template declaration 
"mbsstr_template" cannot be found. An extra "template <>" may be specified on 
this declaration.
"../gllib/string.h", line 1728.23: 1540-0603 (S) The template declaration 
"mbsstr_template" cannot be found. An extra "template <>" may be specified on 
this declaration.
"../gllib/string.h", line 1787.7: 1540-0121 (S) A template cannot have "C" 
linkage.
"../gllib/string.h", line 1789.17: 1540-0603 (S) The template declaration 
"mbspcasecmp_template" cannot be found. An extra "template <>" may be specified 
on this declaration.
"../gllib/string.h", line 1792.23: 1540-0603 (S) The template declaration 
"mbspcasecmp_template" cannot be found. An extra "template <>" may be specified 
on this declaration.
"../gllib/string.h", line 1821.7: 1540-0121 (S) A template cannot have "C" 
linkage.
"../gllib/string.h", line 1823.17: 1540-0603 (S) The template declaration 
"mbscasestr_template" cannot be found. An extra "template <>" may be specified 
on this declaration.
"../gllib/string.h", line 1826.23: 1540-0603 (S) The template declaration 
"mbscasestr_template" cannot be found. An extra "template <>" may be specified 
on this declaration.
gmake[4]: *** [Makefile:26704: test-strings-h-c++.o] Error 1

The cause is that some header files in /usr/include do things like

  extern "C" {
  #include <...>
  }

and one of these system include files ends up doing '#include <string.h>',
which includes Gnulib's string.h.

This patch fixes it.


2025-02-10  Bruno Haible  <br...@clisp.org>

        mbsstr, etc.: Fix compilation in C++ mode on AIX with xlc.
        * lib/string.in.h (mbsstr, mbspcasecmp, mbscasestr): Wrap template
        declarations and definitions in 'extern "C++" { ... }'.

diff --git a/lib/string.in.h b/lib/string.in.h
index 0b7f8cebc6..ac6b459de9 100644
--- a/lib/string.in.h
+++ b/lib/string.in.h
@@ -1182,6 +1182,7 @@ _GL_EXTERN_C char * mbsstr (const char *haystack, const 
char *needle)
 /* Don't silently convert a 'const char *' to a 'char *'.  Programmers want
    compiler warnings for 'const' related mistakes.  */
 #  ifdef __cplusplus
+extern "C++" { /* needed for AIX */
 template <typename T>
   T * mbsstr_template (T* haystack, const char *needle);
 template <>
@@ -1190,6 +1191,7 @@ template <>
 template <>
   inline const char * mbsstr_template (const char *haystack, const char 
*needle)
   { return mbsstr (haystack, needle); }
+}
 #   define mbsstr mbsstr_template
 #  else
 #   if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
@@ -1247,6 +1249,7 @@ _GL_EXTERN_C char * mbspcasecmp (const char *string, 
const char *prefix)
 /* Don't silently convert a 'const char *' to a 'char *'.  Programmers want
    compiler warnings for 'const' related mistakes.  */
 #  ifdef __cplusplus
+extern "C++" { /* needed for AIX */
 template <typename T>
   T * mbspcasecmp_template (T* string, const char *prefix);
 template <>
@@ -1255,6 +1258,7 @@ template <>
 template <>
   inline const char * mbspcasecmp_template (const char *string, const char 
*prefix)
   { return mbspcasecmp (string, prefix); }
+}
 #   define mbspcasecmp mbspcasecmp_template
 #  else
 #   if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
@@ -1282,6 +1286,7 @@ _GL_EXTERN_C char * mbscasestr (const char *haystack, 
const char *needle)
 /* Don't silently convert a 'const char *' to a 'char *'.  Programmers want
    compiler warnings for 'const' related mistakes.  */
 #  ifdef __cplusplus
+extern "C++" { /* needed for AIX */
 template <typename T>
   T * mbscasestr_template (T* haystack, const char *needle);
 template <>
@@ -1290,6 +1295,7 @@ template <>
 template <>
   inline const char * mbscasestr_template (const char *haystack, const char 
*needle)
   { return mbscasestr (haystack, needle); }
+}
 #   define mbscasestr mbscasestr_template
 #  else
 #   if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \




Reply via email to