MINGW-packages icon indicating copy to clipboard operation
MINGW-packages copied to clipboard

UTF8 is not working with MSYS2 Mingw64 UCRT compiler

Open badrelmers opened this issue 6 months ago • 7 comments

Description / Steps to reproduce the issue

I see in multiple places things like that:

UCRT (ucrtbase.dll): Supports UTF-8 in paths, if you enable a UTF-8 locale in your application.

here and here.

I tested with this:

#include <stdio.h>
#include <locale.h>

int main() {
    setlocale(LC_ALL, "en_US.UTF-8"); // Set locale to UTF-8
    FILE *fp = fopen("test_utf8_文件.txt", "r");  
    if (fp) {
        printf("File opened successfully\n");
        fclose(fp);
    } else {
        perror("Error opening file");
    }
    return 0;
}

I compiled it with UCRT64 of Mingw64 of MSYS2 in Windows 7

Expected behavior

it should read the file and print File opened successfully

Actual behavior

when I run it in Windows 7 I get: Error opening file: Invalid argument

Verification

  • [x] I have verified that my MSYS2 is up-to-date before submitting the report (see https://www.msys2.org/docs/updating/)

Windows Version

MINGW64_NT-6.1-7601-WOW64

Are you willing to submit a PR?

No response

badrelmers avatar Jun 26 '25 06:06 badrelmers

I'm not sure this can work on Windows 7. Also, not sure about that. But I think you'd need to link with a manifest to indicate that your application should use UTF-8: https://learn.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page

mmuetzel avatar Jun 26 '25 06:06 mmuetzel

@mmuetzel it seems that the manifest works with newer windows only Microsoft says:

To use this feature on an OS prior to Windows 10, you must use app-local deployment or link statically using version 1803 (10.0.17134.0) of the Windows SDK or later. For Windows 10 operating systems prior to 1803 (10.0.17134.0), only static linking is supported.

badrelmers avatar Jun 26 '25 15:06 badrelmers

it seems that the manifest works with newer windows only

Exactly. That's why I wrote that I'm not sure this can work on Windows 7.

mmuetzel avatar Jun 27 '25 07:06 mmuetzel

Can we close this?

oltolm avatar Aug 29 '25 09:08 oltolm

@oltolm UTF8 and UCRT works fine if i compile with visual studio in windows 7, so it is not a problem of windows 7, this needs to be fixed in mingw I think and hope

badrelmers avatar Aug 29 '25 16:08 badrelmers

@badrelmers what it's saying is you need to static link ucrt for this to work, which for "mingw" is currently not possible. It is not clear if Microsoft's license allows this or not and there do seem to be some UCRT sources published as MIT https://www.nuget.org/packages/Microsoft.Windows.SDK.CRTSource But it does say "Subset" so it might not be useful.

Peter0x44 avatar Aug 29 '25 16:08 Peter0x44

@Peter0x44 yes I see, thank you. please do you know a working way to link statically with UCRT in Mingw?

badrelmers avatar Aug 29 '25 18:08 badrelmers