-fexec-charset オプションについて

実行ファイルの文字セットを指定したいって場合に、GCC では -fexec-charset オプションが使えるみたいです。

・Invocation - The C Preprocessor
https://gcc.gnu.org/onlinedocs/cpp/Invocation.html

  • 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.

※ デフォルトは UTF-8。iconv 使ってるんですね。

・libiconv - GNU Project - Free Software Foundation (FSF)
http://www.gnu.org/software/libiconv/

ちょっと動作確認を。

#include <stdio.h>

void main()
{
  unsigned char s[] = "あ";
  printf("%x %x %x\n",*(s), *(s+1), *(s+2));
}

※ ソース自体は UTF-8 で保存。

■ オプション指定なし

[root@centos62 c]# gcc Test.c
[root@centos62 c]# ./a.out
e3 81 82

■ "SHIFT_JIS" を指定

[root@centos62 c]# gcc -fexec-charset=SHIFT_JIS Test.c
[root@centos62 c]# ./a.out
82 a0 0

■ "EUC-JP" を指定

[root@centos62 c]# gcc -fexec-charset=EUC-JP Test.c
[root@centos62 c]# ./a.out
a4 a2 0


なるほど。以上です。

[ 検証環境 ]
CentOS 6.2
GCC 4.4.7