Paul Eggert did this on 2021-09-07: > * lib/string.in.h, lib/wchar.in.h: > (free): Declare by hand instead of including stdlib.h. > This avoids some namespace pollution.
Unfortunately, when compiling GNU libiconv (or GNU libunistring) with MSVC, it causes this compilation error: /home/bruno/msvc/compile cl -nologo -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -I. -I../../srclib -I.. -I../lib -DDEPENDS_ON_LIBICONV=1 -DDEPENDS_ON_LIBINTL=1 -D_WIN32_WINNT=_WIN32_WINNT_WINXP -I/usr/local/msvc32/include -MD -c -o stat.obj `cygpath -w '../../srclib/stat.c'` stat.c C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\corecrt_malloc.h(85): error C2375: 'rpl_free': redefinition; different linkage .\string.h(628): note: see declaration of 'rpl_free' This was first reported in <https://stackoverflow.com/questions/70525755/> . The problem is that if <stdlib.h> is not included at a certain point, and we fiddle around with the 'free' declaration, and <stdlib.h> is included later, we can easily get a compilation error due to inconsistent declarations of free(). This patch fixes it. 2022-01-04 Bruno Haible <[email protected]> string, wchar: Fix compilation error on MSVC (regression 2021-09-07). * lib/string.in.h (free): Don't redeclare as rpl_free. Instead, redefine _GL_ATTRIBUTE_DEALLOC_FREE to reference rpl_free directly. * lib/wchar.in.h (free): Likewise. diff --git a/lib/string.in.h b/lib/string.in.h index 0c4aefcce..03e6a17a3 100644 --- a/lib/string.in.h +++ b/lib/string.in.h @@ -112,12 +112,26 @@ /* The definition of _GL_WARN_ON_USE is copied here. */ -/* Declare 'free' if needed for _GL_ATTRIBUTE_DEALLOC_FREE. */ -_GL_EXTERN_C void free (void *); +/* Make _GL_ATTRIBUTE_DEALLOC_FREE work, even though <stdlib.h> may not have + been included yet. */ #if @GNULIB_FREE_POSIX@ # if (@REPLACE_FREE@ && !defined free \ && !(defined __cplusplus && defined GNULIB_NAMESPACE)) -# define free rpl_free +/* We can't do '#define free rpl_free' here. */ +_GL_EXTERN_C void rpl_free (void *); +# undef _GL_ATTRIBUTE_DEALLOC_FREE +# define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (rpl_free, 1) +# else +# if defined _MSC_VER +_GL_EXTERN_C void __cdecl free (void *); +# else +_GL_EXTERN_C void free (void *); +# endif +# endif +#else +# if defined _MSC_VER +_GL_EXTERN_C void __cdecl free (void *); +# else _GL_EXTERN_C void free (void *); # endif #endif diff --git a/lib/wchar.in.h b/lib/wchar.in.h index 4db98741a..d7792e5fb 100644 --- a/lib/wchar.in.h +++ b/lib/wchar.in.h @@ -175,12 +175,26 @@ typedef int rpl_mbstate_t; # endif #endif -/* Declare 'free' if needed for _GL_ATTRIBUTE_DEALLOC_FREE. */ -_GL_EXTERN_C void free (void *); +/* Make _GL_ATTRIBUTE_DEALLOC_FREE work, even though <stdlib.h> may not have + been included yet. */ #if @GNULIB_FREE_POSIX@ # if (@REPLACE_FREE@ && !defined free \ && !(defined __cplusplus && defined GNULIB_NAMESPACE)) -# define free rpl_free +/* We can't do '#define free rpl_free' here. */ +_GL_EXTERN_C void rpl_free (void *); +# undef _GL_ATTRIBUTE_DEALLOC_FREE +# define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (rpl_free, 1) +# else +# if defined _MSC_VER +_GL_EXTERN_C void __cdecl free (void *); +# else +_GL_EXTERN_C void free (void *); +# endif +# endif +#else +# if defined _MSC_VER +_GL_EXTERN_C void __cdecl free (void *); +# else _GL_EXTERN_C void free (void *); # endif #endif
