BLOGTIMES
2010/10/20

MinGWでSJISのファイルをコンパイルする

  c  charset 
このエントリーをはてなブックマークに追加

MinGWを使ってSJISのソースコードをコンパイルしようとすると0x5C問題によって、unknown escape sequence: '\216'と言われてしまうことがあります。

test.c

#include <stdio.h> int main(){ printf("表示"); return 0; }

例えば上記のようなコードをコンパイルしようとすると、下記のようになります。

$ gcc test.c -o test test.c: In function 'main': test.c:4:9: warning: unknown escape sequence: '\216'

これがWindowsでGCCを使う際にとても困るのでなんとかする方法がないか調べてみました。

-finput-charsetと-fexec-charsetを使えばいいらしい

どうやらGCCは3.4からソースのエンコーディングが指定できるようになったらしく、デフォルトではUTF-8が期待されているようです。

Invocation - The C Preprocessor

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

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

ということで下記のようにしてみたところ、問題なく実行できることが確認できました。

$ gcc --version gcc.exe (GCC) 4.5.0 Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc test.c -o test -finput-charset=CP932 -fexec-charset=CP932 $ test.exe 表示

    トラックバックについて
    Trackback URL:
    お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
    このエントリへのTrackbackにはこのURLが必要です→https://blog.cles.jp/item/3859
    Trackbacks
    このエントリにトラックバックはありません
    Comments
    愛のあるツッコミをお気軽にどうぞ。[policy]
    古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
    コメントはありません
    Comments Form

    コメントは承認後の表示となります。
    OpenIDでログインすると、即時に公開されます。

    OpenID を使ってログインすることができます。

    Identity URL: Yahoo! JAPAN IDでログイン