在linux上用mingw构建程序并在wine下运行---UTF-8支持

n. *_* m. 5 c wine utf-8 mingw-w64

我有打印一些字符的 C 代码。我希望看到这些字符被打印出来,而不是字母汤,

\n
#include <stdio.h>\nint main() {\n    const char* test = "Greek: \xce\xb1\xce\xb2\xce\xb3\xce\xb4; German: \xc3\x9cbergr\xc3\xb6\xc3\x9fentr\xc3\xa4ger";\n    printf("%s\\n", test);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

什么有效:

\n
    \n
  1. 本机Linux编译:gcc test.c -o test && ./test
  2. \n
  3. Winegcc编译:winegcc test.c o test.exe && ./test.exe
  4. \n
  5. 当输出不是终端时的MinGW64 编译:x86_64-w64-mingw32-gcc -o test.exe test.cpp && wine ./test.exe | cat
  6. \n
\n

所有三个命令都会打印Greek: \xce\xb1\xce\xb2\xce\xb3\xce\xb4; German: \xc3\x9cbergr\xc3\xb6\xc3\x9fentr\xc3\xa4ger到终端。

\n

什么不起作用:

\n
    \n
  1. 当输出是终端时的MinGW64 编译:x86_64-w64-mingw32-gcc -o test.exe test.cpp && wine ./test.exe
  2. \n
\n

这会打印Greek: \xc3\x8e\xc2\xb1\xc3\x8e\xc2\xb2\xc3\x8e\xc2\xb3\xc3\x8e\xc2\xb4; German: \xc3\x83bergr\xc3\x83\xc2\xb6\xc3\x83entr\xc3\x83\xc2\xa4ger到终端。

\n

看起来 MinGW 运行时检测输出是否是终端/控制台,并且执行了完全错误的操作。

\n

我尝试过的:

\n
    \n
  1. system("chcp 65001");\xe2\x80\x94 相同的输出(chcp 命令成功)。
  2. \n
  3. x86_64-w64-mingw32-gcc -fexec-charset=utf8 -finput-charset=utf8\xe2\x80\x94 相同的输出。
  4. \n
  5. SetConsoleOutputCP(CP_UTF8);\xe2\x80\x94 相同的输出。
  6. \n
  7. wineconsole cmd并从那里运行.\\test\xe2\x80\x94 相同的输出。
  8. \n
  9. LANG=en_US.UTF-8 wine ./test.exe\xe2\x80\x94 相同的输出。
  10. \n
  11. _setmode(_fileno(stdout), _O_U8TEXT);\xe2\x80\x94 根本没有输出。
  12. \n
  13. 上述 \xe2\x80\x94 的大多数组合都没有骰子。
  14. \n
\n

我还没有尝试过:

\n
    \n
  1. 在 Wine 下使用 Microsoft 编译器进行编译。
  2. \n
  3. 在 Wine 下使用 MinGW 进行编译。
  4. \n
  5. 使用任一编译器在真实 Windows 下编译和/或运行程序。
  6. \n
\n

如何修复无法正常工作的情况?

\n

小智 2

我查了一下,被告知wprintf()通常适用于“宽”字符格式(例如多于一个字节的字符格式)。我通常不使用 C,所以我也很好奇如何强制 UTF-8 输出。

另外,这locale.h可能会有所帮助:

    // Set the console to support UTF-8 output
    _setmode(_fileno(stdout), _O_U8TEXT);
Run Code Online (Sandbox Code Playgroud)