On 2026/08/16 20:23, Pali Rohár wrote:
> On Sunday 16 August 2026 20:00:25 Zhongteng Gui wrote:
>> On 2026/08/16 19:43, Pali Rohár wrote:
>>> Hello, following simple C++ code:
>>>
>>> #include <string.h>
>>> int main() { return 0; }
>>>
>>> stared generating new compile warnings:
>>>
>>> $ gcc test.cpp
>>> In file included from /usr/local/include/string.h:10,
>>> from test.cpp:1:
>>> /usr/local/include/corecrt_memory.h:17:31: warning: declaration of ‘const
>>> void* memchr(const void*, int, size_t)’ conflicts with built-in declaration
>>> ‘void* memchr(const void*, int, unsigned int)’
>>> [-Wbuiltin-declaration-mismatch]
>>> _CONST_RETURN void *__cdecl memchr(const void *_Buf ,int _Val,size_t
>>> _MaxCount);
>>> ^~~~~~
>>> In file included from test.cpp:1:
>>> /usr/local/include/string.h:63:31: warning: declaration of ‘const char*
>>> strchr(const char*, int)’ conflicts with built-in declaration ‘char*
>>> strchr(const char*, int)’ [-Wbuiltin-declaration-mismatch]
>>> _CONST_RETURN char *__cdecl strchr(const char *_Str,int _Val);
>>> ^~~~~~
>>> /usr/local/include/string.h:87:31: warning: declaration of ‘const char*
>>> strpbrk(const char*, const char*)’ conflicts with built-in declaration
>>> ‘char* strpbrk(const char*, const char*)’ [-Wbuiltin-declaration-mismatch]
>>> _CONST_RETURN char *__cdecl strpbrk(const char *_Str,const char
>>> *_Control);
>>> ^~~~~~~
>>> /usr/local/include/string.h:88:31: warning: declaration of ‘const char*
>>> strrchr(const char*, int)’ conflicts with built-in declaration ‘char*
>>> strrchr(const char*, int)’ [-Wbuiltin-declaration-mismatch]
>>> _CONST_RETURN char *__cdecl strrchr(const char *_Str,int _Ch);
>>> ^~~~~~~
>>> /usr/local/include/string.h:91:31: warning: declaration of ‘const char*
>>> strstr(const char*, const char*)’ conflicts with built-in declaration
>>> ‘char* strstr(const char*, const char*)’ [-Wbuiltin-declaration-mismatch]
>>> _CONST_RETURN char *__cdecl strstr(const char *_Str,const char *_SubStr);
>>> ^~~~~~
>>>
>>> GCC version is 8.3 (it is older one).
>>>
>>> It happens also with -std=c++2a and -std=c++98 options.
>>>
>>> This seems to be related to the change:
>>> "headers: crt: define _CONST_RETURN to const for C++".
>>>
>>> Could you look at it? Maybe the "#define _CONST_RETURN const" needs to
>>> be conditional and not always?
>>>
>>> I have checked memchr declaration also on godbolt.org and this issue
>>> happens with gcc versions less than 15: https://godbolt.org/z/9xe69xxss
>>
>> I think this is a bug in GCC, see
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113706
>> and
>> https://github.com/gcc-mirror/gcc/commit/c3bc2787b8beb7aae67fdf2a7f7271a9a4edca7c
>> It's fixed in GCC 15 at 2024-05-01, no idea why it isn't backported to GCC
>> 14.
>>
>> I've extended the test on godbolt, it seemes that all versions of Clang
>> accept it with no warnings, while GCC 7-14 warns about it.
>>
>> Not sure whether we really want to implement a workaround for this, as it'll
>> be very hacky.
>> I'll try ask GCC whether they'd like to backport this fix to GCC 13.5 and
>> GCC 14.5.
>
> Something needs to be done. As in the current state -Werror will not
> compile any mingw-w64 application which is including string.h on older
> gcc versions.
>
> I would suggest to not define the _CONST_RETURN on gcc pre-15 versions.
> This should be simple check and would let the behavior as it was before
> the change.
I've got a new idea to implement this, similar to glibc[1] but with improvement.
Take strchr as an example, other things are similar
#ifdef __cplusplus
extern "C++" {
const char *__cdecl strchr(const char *, int)
__MINGW_ASM_CALL(strchr);
inline char *__cdecl strchr(char *str, int ch)
{ return const_cast<char *>(strchr(const_cast<const char *>(str), ch)); }
}
#else
char *__cdecl strchr(const char *, int);
#endif
The difference between glibc is that we only define one C++ version with
__asm__ instead of both.
This is used to avoid -Wodr under LTO, see https://godbolt.org/z/nc1ejss3r
[1]: glibc string.h:
https://github.com/gnutools/glibc/blob/master/string/string.h
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public