日期:2014-05-17  浏览次数:21477 次

Windows下Code Blocks字符集设置

Windows下Code Blocks字符集设置

Windows下使用 MinGW + Code Blocks +  wxWidGets 集成开发环境时,经常会遇到中文显示乱码的问题。

在下经过一些尝试,得出了如下的解决方法。

 

1. 编译wxwidGets的命令中,将UNICODE和SHARED均置为1。

例如,mingw32-make -f makefile.gcc BUILD=release SHARED=1 MONOLITHIC=0 UNICODE=1

 

2. 安装完Code Blocks后,选择Settings->Compiler and  Debugger->Compiler Settings->Other options

在里面填写 -finput-charset=GBK  -fexec-charset=UTF-8

第一个选项-finput-charset=GBK ,用于设置gcc预处理器生成的中间源文件的字符集。

使用这个选项的目的,是为了让引用字符串常量的语句(例如 wxMessageBox(_("谢谢您的使用"), _("quit")); ),能够正确编译执行。

 

-finput-charset的具体含义见gcc手册的描述:

-finput-charset=charset

Set the input character set, used for translation from the character set of the input file to the source character set used by GCC. If the locale does not specify, or GCC cannot get this information from the locale, the default is UTF-8. This can be overridden by either the locale or this command line option. Currently the command line option takes precedence if there's a conflict. charset can be any encoding supported by the system's iconv library routine.

 


第二个选项-fexec-charset=UTF-8,用于设置gcc生成的可执行文件中字符数据的字符集。

使用这个选项的目的,是为了让类似如下的代码,能够正确执行。

char *the_strings[] = {"不有中有 " ,    "不无中无 ",    "不色中色 ",   "不空中空 " ,    NULL};

void *haha(void *)
{
    int i = 2;
    gptTextCtrl1->AppendText(wxString::FromUTF8(the_strings[i]));


}

 

-fexec-charset的具体含义见gcc手册的描述:

-fexec-charset=charset

Set the execution character set, used for string and character constants. The default is UTF-8. charset can be any encoding supported by the system's iconv library routine.