内容纲要

mingw 一般情况下使用的 utf8 字符集,调用 windows API 时,使用的是多字节字符集 API (以A结尾)。调用 windows API时还得将 utf8 编码转换为 ansi 编码调用 API,英文无变化。
mingw 调用宽字节 API 需要定义 _UNICODEUNICODE 宏,并链接 -municode

Example

[code language=”cpp”]
#include <wchar.h>
#include <stdio.h>

int
wmain (int argc, wchar_t **argv)
{
wprintf(L"Hello\n");
return 0;
}
[/code]

While it is not necessary to define _UNICODE or UNICODE to compile the above code, -municode is needed for linking because it uses wmain() instead of the traditional main().

[code language=”cpp”]
#define _UNICODE
#define UNICODE
#include <tchar.h>

int
_tmain(int argc, _TCHAR **argv)
{
_tprintf(__T("Hello\n"));
return 0;
}
[/code]

C++ and wmain()

g++ doesn’t know a thing about wmain(), therefore, marking wmain() explicitly as extern “C” is necessary in C++ code, otherwise linkage fails with a cryptic message of undefined reference to wWinMain. Example:

[code language=”cpp”]
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef UNICODE
#define UNICODE
#endif
#include <wchar.h>
#include <stdio.h>

extern "C"
int wmain(int argc, wchar_t** argv)
{
[… code …]
}
[/code]

  • https://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/
  • 发表评论

    电子邮件地址不会被公开。 必填项已用*标注