C++ のコンパイルでエラーが出てはまっています。

環境はmacOSです。

コンピュータ将棋のアルゴリズムというサイトでソースコードが紹介されていたのでそれを動かそうとしています。  
<http://homepage1.nifty.com/Ike/ComShogi/02.html>  
のchapter02フォルダのKomaMoves.cppをコンパイルしようとしています。

まず出たエラーが下記です。

コマンドは`% g++ -o komaMoves KomaMoves.cpp`

[code]

    KomaMoves.cpp:206:67: error: source file is not valid UTF-8
      ...<8B><E0>","<94>n","<97><B4>",
         ^
    KomaMoves.cpp:206:68: error: source file is not valid UTF-8
      ...<E0>","<94>n","<97><B4>",
         ^
    KomaMoves.cpp:206:72: error: source file is not valid UTF-8
      ...<94>n","<97><B4>",
         ^
    KomaMoves.cpp:206:77: error: source file is not valid UTF-8
      ...<97><B4>",
    
[/code]

日本語が書かれている箇所があったのでそこに原因があると思いそこをとりあえず削除して実行できるか確認したところ、続いて下記のエラーが出ました。

[code]

    % g++ -o komaMoves KomaMoves.cpp
    Undefined symbols for architecture x86_64:
      "_main", referenced from:
         implicit entry/start for main executable
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    
[/code]

どうすればコンパイルできるかご教示いただけるとありがたいです。

ソースコード `KomaMoves.cpp`

[code]

    #include "kyokumen.h"
    
    // 方向を示す定数。
    int Direct[12]={
        17,1,-15,16,-16,15,-1,-17,14,-18,18,-14
    };
    
    // 成ることが出来る駒か？
    int CanPromote[]={
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    };
    
    // その方向に動けるか？その方向に飛んで動くものは入れてはいけない。
    int CanMove[12][64]={
        {
        // Direct[0]=17,
        // |／
        //  ￣
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,1,1,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=1,
        // ↓
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,1,0,0,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-15,
        // ＼|
        // ￣
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,1,1,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=16,
        // ←
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-16,
        // →
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=15,
        //  __
        // |＼
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,1,0,1,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-1,
        // ↑
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,1,1,1,1,1,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-17
        // __
        // ／|
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,1,0,1,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=14,先手の桂馬
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-18,先手の桂馬
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-14,後手の桂馬
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=18,後手の桂馬
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        }
    };
    
    // その方向に飛んで動くことが出来るか？
    // 飛車角香車と龍と馬しかそういう駒はない
    int CanJump[12][64]={
        // Direct[0]=17,
        // ＼|
        // ￣
        {
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=1,
        // ↓
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-15,
        // ＼|
        // ￣
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=16,
        // ←
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-16,
        // →
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=15,
        //  __
        // |＼
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-1,
        // ↑
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-17
        // __
        // ／|
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=14,先手の桂馬飛び
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-18,先手の桂馬飛び
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=-14,後手の桂馬飛び
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        },{
        // Direct[0]=18,後手の桂馬飛び
    //  空空空空空空空空空空空空空空空空空歩香桂銀金角飛王と杏圭全金馬龍
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    //  空歩香桂銀金角飛王と杏圭全金馬龍壁空空空空空空空空空空空空空空空
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        }
    };
    
    // 駒を盤面に表示するための文字列
    char *komaStr[]={
    "   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ",
    "   "," 歩"," 香"," 桂"," 銀"," 金"," 角"," 飛"," 王"," と"," 杏"," 圭"," 全"," 金"," 馬"," 龍",
    "   ","v歩","v香","v桂","v銀","v金","v角","v飛","v王","vと","v杏","v圭","v全","v金","v馬","v龍",
    " 壁","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ","   ",
    };
    
    // 駒の種類を表示するための文字列
    char *komaStr2[]={
    "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ",
    "  ","歩","香","桂","銀","金","角","飛","王","と","杏","圭","全","金","馬","龍",
    "  ","歩","香","桂","銀","金","角","飛","王","と","杏","圭","全","金","馬","龍",
    " 壁","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ",
    };
    
[/code]


CC BY-SA 3.0
2015-12-05T05:22:54.680
19650
2015-12-05T09:02:27.130
2015-12-05T05:33:17.660
7232
13538
question
c++ macos
C++のコンパイルがうまくいきません


文字化けはソースの文字コードがSJISのためです。リンカーのエラーはこのファイルだけではなく、他のファイルも同時にコンパイルが必要なためです。makefile.gccが用意されていますので、`make
-f makefile.gcc`を実行しましょう。

文字コードの変換前に、gitにファイルを全部チェックインして、次の手順が失敗してもすぐに戻せるようにします(^^;

[code]

    cd chapter02
    git init
    git add -A
    git commit -m "initial commit"
    
[/code]

次のシェルスクリプトを走らせ、文字コードを修正してからコンパイルします。

[code]

    #!/bin/bash
    FILES="*.cpp *.h"
    for f in $FILES
    do
        iconv -f cp932 -t utf8 $f | tr -d \\r > $f+ && mv $f+ $f
    done
    make -f makefile.gcc
    
[/code]

実行するには

[code]

    ./shogi
    
[/code]

出力

[code]

    $ ./shogi
    持ち駒：なし
      ９ ８ ７ ６ ５ ４ ３ ２ １
    +---------------------------+
    |v香v桂v銀v金v王v金v銀v桂v香|�
    |   v飛               v角   |��
    |v歩v歩v歩v歩v歩v歩v歩v歩v歩|��
    |                           |�
    |                           |��
    |                           |��
    | 歩 歩 歩 歩 歩 歩 歩 歩 歩|�
    |    角                飛   |��
    | 香 桂 銀 金 王 金 銀 桂 香|��
    +---------------------------+
    持ち駒：なし
    
[/code]

まだ文字化けが発生していますが、ここから先はご自身で調査してみてください。

EDIT: 文字化けは全角文字が2バイトであることを前提にしたコードが理由でした。UTF-8で3バイトだとして、次のパッチを当てたら正しく表示されました。

[code]

    diff --git a/kyokumen.cpp b/kyokumen.cpp
    index e984cff..3498275 100644
    --- a/kyokumen.cpp
    +++ b/kyokumen.cpp
    @@ -767,7 +767,7 @@ void Kyokumen::FPrint(FILE *fp)
                    for(x=9;x>=1;x--) {
                            fprintf(fp,komaStr[ban[x*16+y]]);
                    }
    -               fprintf(fp,"|%2.2s","一二三四五六七八九" + y*2-2);
    +               fprintf(fp,"|%3.3s","一二三四五六七八九" + y*3-3);
                    fprintf(fp,"\n");
            }
            fprintf(fp,"+---------------------------+\n");
    @@ -776,7 +776,7 @@ void Kyokumen::FPrint(FILE *fp)
            for (x = SHI; x >= SFU; x--) {
                    if (Hand[x] > 1) {
                            y = 1;
    -                       fprintf(fp,"%s%2.2s", komaStr2[x], "一二三四五六七八九101112131415161718"+2*Hand[x]-2);
    +                       fprintf(fp,"%s%3.3s", komaStr2[x], "一二三四五六七八九101112131415161718"+3*Hand[x]-3);
                    } else if (Hand[x] == 1) {
                            y = 1;
                            fprintf(fp,"%s", komaStr2[x]);
    
[/code]

結果: フォントが等幅でないので？崩れて見えるかもしれませんが、端末では綺麗に表示されました。

[code]

    $ ./shogi
    持ち駒：なし
      ９ ８ ７ ６ ５ ４ ３ ２ １
    +---------------------------+
    |v香v桂v銀v金v王v金v銀v桂v香|一
    |   v飛               v角   |二
    |v歩v歩v歩v歩v歩v歩v歩v歩v歩|三
    |                           |四
    |                           |五
    |                           |六
    | 歩 歩 歩 歩 歩 歩 歩 歩 歩|七
    |    角                飛   |八
    | 香 桂 銀 金 王 金 銀 桂 香|九
    +---------------------------+
    持ち駒：なし
    
[/code]


CC BY-SA 3.0
2015-12-05T06:17:25.730
19652
2015-12-05T07:15:05.807
2015-12-05T07:15:05.807
7837
7837
19650
answer


※ 以下の回答は Ubuntu Linux 15.10 で gcc 4.9.3 を使用する場合です。Mac OSX + gcc
では期待する動作にはならないかもしれません。

一応、`gcc/g++` で `shift-jis` エンコーディングなソースコードを取り扱う事は可能です。

**gcc(1)**

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

なので、コンパイルオプションに `-finput-charset=CP932 -fexec-charset=CP932` を追加すればコンパイル可能です。

**makefile.gcc**

[code]

    CFLAGS = -finput-charset=CP932 -fexec-charset=CP932
    
    all: shogi
    objs= KomaMoves.o kyokumen.o main.o Te.o
    
    shogi: $(objs)
        g++ -o shogi $(objs)
    
    KomaMoves.o:    kyokumen.h KomaMoves.cpp
        gcc $(CFLAGS) -D_GCC_ -c KomaMoves.cpp
    
    kyokumen.o: kyokumen.h kyokumen.cpp
        gcc $(CFLAGS) -D_GCC_ -c kyokumen.cpp
    
    Te.o:   kyokumen.h Te.cpp
        gcc $(CFLAGS) -D_GCC_ -c Te.cpp
    
    main.o: main.h kyokumen.h main.cpp
        gcc $(CFLAGS) -D_GCC_ -c main.cpp
    
[/code]

これで作成する shogi コマンドは shift-jis エンコーディングな文字列を出力しますので、それらを正常に表示できる端末が必要になります。以下は
Shift_JIS Mode にした kterm で shogi コマンドを実行した際のスクリーンショットです。

[![shogi](https://i.stack.imgur.com/qINAu.png)](https://i.stack.imgur.com/qINAu.png)


CC BY-SA 3.0
2015-12-05T09:02:27.130
19659
2015-12-05T09:02:27.130
19650
answer