📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

 
(同じ利用者による、間の17版が非表示)
108行目: 108行目:
                 --enable-languages=c,c++,fortran \  # C, C++, Fortranをインストールする場合
                 --enable-languages=c,c++,fortran \  # C, C++, Fortranをインストールする場合
                 --disable-bootstrap                # 3-stage bootstrap buildを無効化する場合
                 --disable-bootstrap                # 3-stage bootstrap buildを無効化する場合
                --enable-default-pie                # PIEを有効にする場合
  make -j $(nproc)
  make -j $(nproc)
  make install-strip
  make install-strip
120行目: 121行目:
                 --enable-languages=c,c++,fortran \  # C, C++, Fortranをインストールする場合
                 --enable-languages=c,c++,fortran \  # C, C++, Fortranをインストールする場合
                 --disable-bootstrap              \  # 3-stage bootstrap buildを無効化する場合
                 --disable-bootstrap              \  # 3-stage bootstrap buildを無効化する場合
                --enable-default-pie            \  # PIEを有効にする場合
                 --with-gmp=<GMP, MPC MPFR, ISLのインストールディレクトリ>  \
                 --with-gmp=<GMP, MPC MPFR, ISLのインストールディレクトリ>  \
                 --with-mpc=<GMP, MPC MPFR, ISLのインストールディレクトリ>  \
                 --with-mpc=<GMP, MPC MPFR, ISLのインストールディレクトリ>  \
145行目: 147行目:
  make install-strip
  make install-strip
<br>
<br>
インストールしたGCCへ環境変数のパスを通す。<br>
~/.profileファイル等に環境変数を設定する。<br>
  vi ~/.profile
  vi ~/.profile
<br>
<syntaxhighlight lang="sh">
# ~/.profileファイル
   
   
# .profileファイル
  export export PATH="/<GCCのインストールディレクトリ>/bin:$PATH"
  export export PATH="/<GCCのインストールディレクトリ>/bin:$PATH"
  export LD_LIBRARY_PATH="/<GCCのインストールディレクトリ>/lib64:$LD_LIBRARY_PATH"
  export LD_LIBRARY_PATH="/<GCCのインストールディレクトリ>/lib64:$LD_LIBRARY_PATH"
</syntaxhighlight>
<br>
<br>
最後に、PCを再起動する。<br>
最後に、PCを再起動する。<br>
<br>
<br>
==== GCCの設定 ====
==== GCCの設定 ====
GCCをソースコードからインストールした場合、共有ライブラリが/<GCCのインストールディレクトリ>/lib64に作成されるが、環境変数のパスが設定されていない。<br>
GCCをソースコードからインストールした場合、共有ライブラリが <u>/<GCCのインストールディレクトリ>/lib64</u> ディレクトリ内に生成されるが、環境変数が設定されていない。<br>
そのため、以下に示すいずれかの方法で環境変数のパスを通す必要がある。<br>
そのため、以下に示すいずれかの方法で環境変数のパスを通す必要がある。<br>
<br>
* システム全体に設定する場合
* システム全体に設定する場合
*: /etc/ld.so.confファイル
*: /etc/ld.so.confファイル
* ユーザごとに設定する場合
* ユーザごとに設定する場合
*: ~/.profileファイル等
*: ~/.profileファイル等
*: 環境変数<code>LD_LIBRARY_PATH</code>を設定する。
*: 環境変数 <code>LD_LIBRARY_PATH</code> を設定する。
<br>
<br>
===== システム全体に設定する場合 =====
===== システム全体に設定する場合 =====
/etc/ld.so.confファイルに<code>/<GCCのインストールディレクトリ/lib64</code>を追記することで、システム全体がライブラリを認識する。<br>
/etc/ld.so.confファイルに <u>/<GCCのインストールディレクトリ/lib64</u> を追記することで、システム全体がライブラリを認識する。<br>
  sudo vi /etc/ld.so.conf
  sudo vi /etc/ld.so.conf
<br>
<br>
174行目: 180行目:
  ldconfig -v
  ldconfig -v
<br>
<br>
<code>ldconfig</code>コマンドを実行した時に表示される情報から、<br>
<code>ldconfig</code> コマンドを実行した時に表示される情報から、<br>
以下に示すように、<code>/<GCCのインストールディレクトリ/lib64</code>ディレクトリが読み込まれていることを確認する。<br>
以下に示すように、<u>/<GCCのインストールディレクトリ/lib64</u> ディレクトリが読み込まれていることを確認する。<br>
  /<GCCのインストールディレクトリ>/lib64:
  /<GCCのインストールディレクトリ>/lib64:
  ldconfig: /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py is not an ELF file - it has the wrong magic bytes at the start.
  ldconfig: /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py is not an ELF file - it has the wrong magic bytes at the start.
186行目: 192行目:
         libmpxwrappers.so.2 -> libmpxwrappers.so.2.0.1
         libmpxwrappers.so.2 -> libmpxwrappers.so.2.0.1
<br>
<br>
<code>ldconfig</code>コマンドを実行した結果、以下に示すようなメッセージが表示される場合がある。<br>
<code>ldconfig</code> コマンドを実行した結果、以下に示すようなメッセージが表示される場合がある。<br>
  ldconfig: /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py is not an ELF file - it has the wrong magic bytes at the start.
  ldconfig: /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py is not an ELF file - it has the wrong magic bytes at the start.
<br>
<br>
これは、libstdc++.so.6.0.25-gdb.pyは、pythonのスクリプトであるが、これを共有ライブラリとして認識していることが原因である。<br>
これは、libstdc++.so.6.0.25-gdb.pyはpythonのスクリプトであるが、これを共有ライブラリとして認識していることが原因である。<br>
  file /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py
  file /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py
   
   
195行目: 201行目:
<br>
<br>
対処方法としては、該当ファイルの名前を変更して、共有ライブラリと認識されないようする。<br>
対処方法としては、該当ファイルの名前を変更して、共有ライブラリと認識されないようする。<br>
  mv /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py  /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py_org
  mv /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py  \
    /<GCCのインストールディレクトリ>/lib64/libstdc++.so.6.0.25-gdb.py_org
<br>
<br>
再度、<code>ldconfig</code>コマンドを実行する。<br>
再度、<code>ldconfig</code> コマンドを実行する。<br>
  ldconfig -v
  ldconfig -v
<br>
<br>
また、RHELおよびSUSEの場合、/etc/ld.so.confファイルに<code>include ld.so.conf.d/*.conf</code>と設定されている。<br>
また、RHELおよびSUSEの場合、/etc/ld.so.confファイルに <u>include ld.so.conf.d/*.conf</u> と設定されている。<br>
これは、ld.so.conf.dディレクトリ内のconf拡張子をもつファイルの内容を読み込むという意味である。<br>
これは、ld.so.conf.dディレクトリ内のconf拡張子をもつファイルの内容を読み込むという意味である。<br>
<br>
<br>
229行目: 236行目:
上記の設定を反映させるため、PCを再起動または再ログインする。<br>
上記の設定を反映させるため、PCを再起動または再ログインする。<br>
<br>
<br>
==== libstdc++を入れ替える場合 ====
==== libstdc++を入れ替える場合 ====
<u>この手順を行う場合、システムが不安定になる可能性があることに注意する。</u><br>
<u>この手順を行う場合、システムが不安定になる可能性があることに注意する。</u><br>
262行目: 268行目:
  sudo zypper install expect-devel gmp-devel mpc-devel mpfr-devel
  sudo zypper install expect-devel gmp-devel mpc-devel mpfr-devel
<br>
<br>
==== PinePhone (AArch64) (システムルートあり) ====
==== PinePhone (AArch64) (システムルートあり) ====
まず、[https://ftp.gnu.org/gnu/binutils/ GNUの公式Webサイト]から、Binutilsをダウンロードする。<br>
まず、[https://ftp.gnu.org/gnu/binutils/ GNUの公式Webサイト]から、Binutilsをダウンロードする。<br>
274行目: 279行目:
クロスコンパイラ向けBinutilsのビルドおよびインストールする。<br>
クロスコンパイラ向けBinutilsのビルドおよびインストールする。<br>
<u>インストールディレクトリは、クロスコンパイラ向けGCCのインストールディレクトリと同じディレクトリであることに注意する。</u><br>
<u>インストールディレクトリは、クロスコンパイラ向けGCCのインストールディレクトリと同じディレクトリであることに注意する。</u><br>
  ../configure CFLAGS="-g0 -O3 -fstack-protector-strong"                                         \
  ../configure CFLAGS="-g0 -O3 -fstack-protector-strong"       \ # バッファオーバーフロー攻撃に対する中程度の保護を有効化
               CXXFLAGS="-g0 -O3 -fstack-protector-strong"                                       \
               CXXFLAGS="-g0 -O3 -fstack-protector-strong"     \ # バッファオーバーフロー攻撃に対する中程度の保護を有効化
               --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>                                   \
               --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
               --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=aarch64-linux-gnu \
               --build=x86_64-pc-linux-gnu \
               --disable-gdb --disable-nls --disable-multilib --disable-werror                   \
              --host=x86_64-pc-linux-gnu \
               --enable-gold --enable-lto --enable-plugins --enable-relro                       \
              --target=aarch64-linux-gnu \
               --disable-gdb               \  # GDBサポートを無効化
              --disable-nls               \  # 国際化サポートを無効化
              --disable-multilib         \  # マルチライブラリサポートを無効化
              --disable-werror           \ # 警告をエラーとして扱わない
               --enable-gold               \  # GOLDリンカを有効化 (高速な代替リンカ)
              --enable-lto               \  # リンク時最適化を有効化
              --enable-plugins           \  # プラグインサポートを有効化
              --enable-relro             \ # RELROセキュリティ機能を有効化 (リロケーションの読み取り専用化)
               --with-sysroot=<ターゲットのシステムルートディレクトリ>
               --with-sysroot=<ターゲットのシステムルートディレクトリ>
   
   
294行目: 307行目:
クロスコンパイラ向けGCCをビルドする場合、ネイティブ向けGCCとバージョンを合わせた方がよい。<br>
クロスコンパイラ向けGCCをビルドする場合、ネイティブ向けGCCとバージョンを合わせた方がよい。<br>
  export PATH="/<上記でインストールしたBinutilsのインストールディレクトリ>/bin:$PATH" && \
  export PATH="/<上記でインストールしたBinutilsのインストールディレクトリ>/bin:$PATH" && \
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス>                 \
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス>             \
  ../configure CFLAGS="-g0 -O3 -fstack-protector-strong"           \
  ../configure -v                                            \
               CXXFLAGS="-g0 -O3 -fstack-protector-strong"         \
              CFLAGS="-g0 -O3 -fstack-protector-strong"     \
               -v --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
               CXXFLAGS="-g0 -O3 -fstack-protector-strong"   \
               --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=aarch64-linux-gnu \
               --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
               --enable-languages=c,c++ --disable-bootstrap --disable-multilib --disable-nls     \
               --build=x86_64-pc-linux-gnu \
              --host=x86_64-pc-linux-gnu \
              --target=aarch64-linux-gnu \
               --enable-languages=c,c++   \
              --disable-bootstrap         \
              --disable-multilib         \
              --disable-nls               \
               --with-sysroot=<ターゲットのシステムルートディレクトリ>
               --with-sysroot=<ターゲットのシステムルートディレクトリ>
   
   
305行目: 324行目:
  make install-strip
  make install-strip
<br>
<br>
 
==== Raspberry Pi 32bit (システムルートあり) ====
==== Rsdpberry Pi 32bit (システムルートあり) ====
まず、[https://ftp.gnu.org/gnu/binutils/ GNUの公式Webサイト]から、Binutilsをダウンロードする。<br>
まず、[https://ftp.gnu.org/gnu/binutils/ GNUの公式Webサイト]から、Binutilsをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
ダウンロードしたファイルを解凍する。<br>
387行目: 405行目:
  make install-strip
  make install-strip
<br>
<br>
==== Rsdpberry Pi 64bit (システムルートあり) ====
 
==== Raspberry Pi 64bit (システムルートあり) ====
まず、[https://ftp.gnu.org/gnu/binutils/ GNUの公式Webサイト]から、Binutilsをダウンロードする。<br>
まず、[https://ftp.gnu.org/gnu/binutils/ GNUの公式Webサイト]から、Binutilsをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
ダウンロードしたファイルを解凍する。<br>
470行目: 489行目:
               --build=x86_64-pc-linux-gnu        \
               --build=x86_64-pc-linux-gnu        \
               --host=x86_64-pc-linux-gnu        \
               --host=x86_64-pc-linux-gnu        \
               --target=riscv64-unknown-linux-gnu \
               --target=riscv64-linux-gnu \
               --disable-nls                      \
               --disable-nls                      \
               --disable-werror                  \
               --disable-werror                  \
477行目: 496行目:
  make -j $(nproc)
  make -j $(nproc)
  make install
  make install
<br>
インストールされているBinutilsのバージョンを確認するには、以下に示すコマンドを実行します。<br>
ld --version
<br>
<br>
次に、[https://gcc.gnu.org GCCの公式Webサイト]にアクセスして、GCCのソースコードをダウンロードして解凍する。<br>
次に、[https://gcc.gnu.org GCCの公式Webサイト]にアクセスして、GCCのソースコードをダウンロードして解凍する。<br>
489行目: 511行目:
               --build=x86_64-pc-linux-gnu        \
               --build=x86_64-pc-linux-gnu        \
               --host=x86_64-pc-linux-gnu        \
               --host=x86_64-pc-linux-gnu        \
               --target=riscv64-unknown-linux-gnu \
               --target=riscv64-linux-gnu \
               --enable-languages=c,c++          \
               --enable-languages=c,c++          \
               --disable-multilib                \
               --disable-multilib                \
618行目: 640行目:
   
   
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
               --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=h8300-elf \
               --build=x86_64-pc-linux-gnu \
               --disable-gdb --disable-nls --disable-multilib --disable-werror \
              --host=x86_64-pc-linux-gnu \
              --target=h8300-elf         \
               --disable-gdb               \
              --disable-nls               \
              --disable-multilib         \
              --disable-werror           \
               --enable-lto
               --enable-lto
  make -j $(nproc)
  make -j $(nproc)
629行目: 656行目:
  cd gcc-<バージョン>
  cd gcc-<バージョン>
<br>
<br>
さらに、[https://sourceware.org/newlib/ NewLibの公式Webサイト]、または、[https://github.com/bminor/newlib/ NewLibのGithub]にアクセスして、NewLibのソースコードをダウンロードする。<br>
さらに、[https://sourceware.org/newlib/ Newlibの公式Webサイト]、または、[https://github.com/bminor/newlib/ NewlibのGithub]にアクセスして、Newlibのソースコードをダウンロードする。<br>
もし、NewLibの公式Webサイトからファイルをダウンロードできない場合は、<code>wget</code>コマンド等を使用してダウンロードする。<br>
もし、Newlibの公式Webサイトからファイルをダウンロードできない場合は、<code>wget</code>コマンド等を使用してダウンロードする。<br>
  wget ftp://sourceware.org/pub/newlib/newlib-<バージョン>.tar.gz
  wget ftp://sourceware.org/pub/newlib/newlib-<バージョン>.tar.gz
<br>
<br>
<u>ただし、現在(2022年11月現在)、NewLibの公式Webサイトからダウンロードしたファイルを使用するとビルドに失敗するため、Githubからダウンロードすること。</u><br>
<u>ただし、現在(2022年11月現在)、Newlibの公式Webサイトからダウンロードしたファイルを使用するとビルドに失敗するため、Githubからダウンロードすること。</u><br>
<br>
<br>
ダウンロードしたファイルを解凍する。<br>
ダウンロードしたファイルを解凍する。<br>
648行目: 675行目:
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス> \
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス> \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>  \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>  \
               --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=h8300-elf \
               --build=x86_64-pc-linux-gnu \
               --enable-languages=c,c++ \
              --host=x86_64-pc-linux-gnu \
               --disable-bootstrap --disable-nls --disable-werror --enable-lto --enable-gold \
              --target=h8300-elf         \
               --with-newlib \
               --enable-languages=c,c++   \
               --with-headers=/<上記でダウンロードしたNewLibのディレクトリ>/newlib/libc/include
               --disable-bootstrap         \
              --disable-nls               \
              --disable-werror           \
              --enable-lto               \
              --enable-gold               \
               --with-newlib               \
               --with-headers=/<上記でダウンロードしたNewlibのディレクトリ>/newlib/libc/include
  make -j $(nproc)
  make -j $(nproc)
  make install
  make install
660行目: 693行目:
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス> \
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス> \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>  \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>  \
               --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=h8300-elf \
               --build=x86_64-pc-linux-gnu \
               --enable-languages=c \
              --host=x86_64-pc-linux-gnu \
               --disable-bootstrap --disable-nls --disable-shared --enable-static --enable-lto --enable-gold \
              --target=h8300-elf         \
               --with-newlib \
               --enable-languages=c       \
               --with-headers=/<上記でダウンロードしたNewLibのディレクトリ>/newlib/libc/include
               --disable-bootstrap         \
              --disable-nls               \
              --disable-shared           \
              --enable-static             \
              --disable-libstdc__-v3      \
              --enable-lto               \
              --enable-gold               \
               --with-newlib               \
               --with-headers=/<上記でダウンロードしたNewlibのディレクトリ>/newlib/libc/include
  make -j $(nproc)
  make -j $(nproc)
  make install
  make install
<br>
<br>
次に、NewLibをビルドおよびインストールする。<br>
次に、Newlibをビルドおよびインストールする。<br>
  mkdir build && cd build
  mkdir build && cd build
   
   
675行目: 715行目:
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス> \
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス> \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>  \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>  \
               --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=h8300-elf
               --build=x86_64-pc-linux-gnu \
              --host=x86_64-pc-linux-gnu \
              --target=h8300-elf
  make -j $(nproc)
  make -j $(nproc)
  make install
  make install
689行目: 732行目:
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス> \
  CC=<ネイティブ向けGCCのパス> CXX=<ネイティブ向けG++のパス> \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>  \
  ../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>  \
               --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=h8300-elf \
               --build=x86_64-pc-linux-gnu \
               --enable-languages=c,c++ \
              --host=x86_64-pc-linux-gnu \
               --disable-bootstrap --disable-nls --disable-shared --enable-static --disable-libstdc__-v3 --enable-lto --enable-gold \
              --target=h8300-elf         \
               --with-newlib \
               --enable-languages=c,c++   \
               --with-headers=/<上記でダウンロードしたNewLibのディレクトリ>/newlib/libc/include
               --disable-bootstrap         \
              --disable-nls               \
              --disable-shared           \
              --enable-static             \
              --disable-libstdc__-v3     \
              --enable-lto               \
              --enable-gold               \
               --with-newlib               \
               --with-headers=/<上記でダウンロードしたNewlibのディレクトリ>/newlib/libc/include
make -j $(nproc)
make install
<br>
==== MinGW-w64 (Windows 64bit) ====
MinGW-w64を使用して、Linux上でWindows 64bit向けバイナリを生成するクロスコンパイラを構築する。<br>
ターゲットトリプレットは <u>x86_64-w64-mingw32</u>、例外処理モデルは <u>SEH</u>、スレッドモデルは <u>POSIX</u> を使用する。<br>
<br>
まず、[https://sourceforge.net/projects/mingw-w64/files/ MinGW-w64のSourceForge]から、MinGW-w64のソースコードをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
tar xf mingw-w64-v<バージョン>.tar.bz2
<br>
===== Binutilsのインストール =====
[https://ftp.gnu.org/gnu/binutils/ GNUの公式Webサイト]から、Binutilsをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
tar xf binutils-<バージョン>.tar.xz
cd binutils-<バージョン>
<br>
ビルドディレクトリを作成する。<br>
mkdir build && cd build
<br>
クロスコンパイラ向けBinutilsのビルドおよびインストールする。<br>
<u>インストールディレクトリは、クロスコンパイラ向けGCCのインストールディレクトリと同じディレクトリであることに注意する。</u><br>
../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-pc-linux-gnu  \
              --target=x86_64-w64-mingw32  \
              --disable-nls                \
              --disable-werror            \
              --disable-multilib          \
              --enable-lto                \
              --enable-gold                \
              --enable-plugins
make -j $(nproc)
make install
<br>
===== MinGW-w64ヘッダのインストール =====
MinGW-w64のヘッダファイルをインストールする。<br>
<code>--with-default-win32-winnt=0x0A00</code> オプションにより、Windows 10以降をターゲットとする。<br>
<br>
<center>
{| class="wikitable"
|+ _WINNT_WINxxマクロの値
! Windowsバージョン !! _WIN32_WINNTバージョン定数
|-
| Windows 2000 || 0x0500
|-
| Windows XP || 0x0501
|-
| Windows Server 2003 || 0x0502
|-
| Windows Vista / Server 2008 || 0x0600
|-
| Windows 7 / Server 2008 R2 || 0x0601
|-
| Windows 8 / Server 2012 || 0x0602
|-
| Windows 8.1 / Server 2012 R2 || 0x0603
|-
| Windows 10 / Server 2016 || 0x0A00
|-
| Windows 11 / Server 2022 || 0x0A00
|}
</center>
<br>
cd /<MinGW-w64のソースコードがあるディレクトリ>/mingw-w64-headers
mkdir build && cd build
../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>/x86_64-w64-mingw32 \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-w64-mingw32    \
              --with-default-win32-winnt=0x0A00
make install
<br>
ターゲットディレクトリにシンボリックリンクを作成する。<br>
ln -s /<クロスコンパイラ向けGCCのインストールディレクトリ>/x86_64-w64-mingw32 \
      /<クロスコンパイラ向けGCCのインストールディレクトリ>/mingw
<br>
===== GCCコアコンパイラ (最小限のGCCコンパイラ) のインストール =====
[https://gcc.gnu.org GCCの公式Webサイト]にアクセスして、GCCのソースコードをダウンロードして解凍する。<br>
または、以下のコマンドを実行してダウンロードする。<br>
wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-<バージョン>/gcc-<バージョン>.tar.xz
tar xf gcc-<バージョン>.tar.xz
cd gcc-<バージョン>
<br>
まず、GCCコアコンパイラ (最小限のGCCコンパイラ) を生成する。<br>
<u>このコンパイラは、CRT および winpthreads のビルドに使用する。</u><br>
mkdir build && cd build
export PATH="/<クロスコンパイラ向けGCCのインストールディレクトリ>/bin:$PATH"
../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-pc-linux-gnu  \
              --target=x86_64-w64-mingw32  \
              --enable-languages=c,c++    \
              --disable-multilib          \
              --disable-nls                \
              --disable-shared            \
              --disable-werror            \
              --enable-lto
make all-gcc -j $(nproc)
make install-gcc
<br>
===== MinGW-w64 CRTのインストール =====
MinGW-w64のCRT (Cランタイムライブラリ) をビルドおよびインストールする。<br>
cd /<MinGW-w64のソースコードがあるディレクトリ>/mingw-w64-crt
mkdir build && cd build
export PATH="<クロスコンパイラ向けGCCのインストールディレクトリ>/bin:$PATH"
../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>/x86_64-w64-mingw32 \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-w64-mingw32    \
              --with-sysroot=<クロスコンパイラ向けGCCのインストールディレクトリ> \
              --enable-lib64              \
              --disable-lib32
make -j $(nproc)
make install
<br>
===== winpthreadsのインストール =====
POSIXスレッドサポートのためのwinpthreadsライブラリをビルドおよびインストールする。<br>
cd /<MinGW-w64のソースコードがあるディレクトリ>/mingw-w64-libraries/winpthreads
mkdir build && cd build
export PATH="/<クロスコンパイラ向けGCCのインストールディレクトリ>/bin:$PATH"
../configure --prefix=/<クロスコンパイラ向けGCCのインストールディレクトリ>/x86_64-w64-mingw32 \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-w64-mingw32    \
              --enable-static              \
              --enable-shared
make -j $(nproc)
make install
<br>
=====  GCCの最終インストール =====
全てのライブラリをインストールした後、完全な機能を持つGCCをビルドおよびインストールする。<br>
cd <GCCのソースコードがあるディレクトリ>/build
# 初期ビルドのディレクトリを削除して再作成する場合
rm -rf *
export PATH="<クロスコンパイラ向けGCCのインストールディレクトリ>/bin:$PATH"
../configure --prefix=/<クロスコンパイラ向けGCCのインストールディレクトリ> \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-pc-linux-gnu  \
              --target=x86_64-w64-mingw32  \
              --enable-languages=c,c++    \
              --disable-multilib          \
              --disable-nls                \
              --disable-werror            \
              --enable-shared              \
              --enable-threads=posix      \
              --enable-lto                \
              --with-dwarf2
make -j $(nproc)
make install-strip
<br>
==== MinGW-w64 (Windows 32bit) ====
MinGW-w64を使用して、Linux上でWindows 32bit向けバイナリを生成するクロスコンパイラを構築する。<br>
ターゲットトリプレットは <u>i686-w64-mingw32</u>、例外処理モデルは <u>DWARF-2</u>、スレッドモデルは <u>POSIX</u> を使用する。<br>
<br>
まず、[https://sourceforge.net/projects/mingw-w64/files/ MinGW-w64のSourceForge]から、MinGW-w64のソースコードをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
tar xf mingw-w64-v<バージョン>.tar.bz2
<br>
===== Binutilsのインストール =====
[https://ftp.gnu.org/gnu/binutils/ GNUの公式Webサイト]から、Binutilsをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
tar xf binutils-<バージョン>.tar.xz
cd binutils-<バージョン>
<br>
ビルドディレクトリを作成する。<br>
mkdir build && cd build
<br>
クロスコンパイラ向けBinutilsをビルドおよびインストールする。<br>
<u>インストールディレクトリは、クロスコンパイラ向けGCCのインストールディレクトリと同じディレクトリであることに注意する。</u><br>
../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-pc-linux-gnu  \
              --target=i686-w64-mingw32    \
              --disable-nls                \
              --disable-werror            \
              --disable-multilib          \
              --enable-lto                \
              --enable-gold                \
              --enable-plugins
make -j $(nproc)
make install
<br>
===== MinGW-w64ヘッダのインストール =====
MinGW-w64のヘッダファイルをインストールする。<br>
以下の例では、<code>--with-default-win32-winnt=0x0A00</code> オプションにより、Windows 10以降をターゲットとしている。<br>
<br>
<center>
{| class="wikitable"
|+ _WINNT_WINxxマクロの値
! Windowsバージョン !! _WIN32_WINNTバージョン定数
|-
| Windows 2000 || 0x0500
|-
| Windows XP || 0x0501
|-
| Windows Server 2003 || 0x0502
|-
| Windows Vista / Server 2008 || 0x0600
|-
| Windows 7 / Server 2008 R2 || 0x0601
|-
| Windows 8 / Server 2012 || 0x0602
|-
| Windows 8.1 / Server 2012 R2 || 0x0603
|-
| Windows 10 / Server 2016 || 0x0A00
|-
| Windows 11 / Server 2022 || 0x0A00
|}
</center>
<br>
cd /<MinGW-w64のソースコードがあるディレクトリ>/mingw-w64-headers
mkdir build && cd build
../configure --prefix=/<クロスコンパイラ向けGCCのインストールディレクトリ>/i686-w64-mingw32 \
              --build=x86_64-pc-linux-gnu  \
              --host=i686-w64-mingw32      \
              --with-default-win32-winnt=0x0A00
make install
<br>
ターゲットディレクトリにシンボリックリンクを作成する。<br>
ln -s /<クロスコンパイラ向けGCCのインストールディレクトリ>/i686-w64-mingw32 \
      /<クロスコンパイラ向けGCCのインストールディレクトリ>/mingw
<br>
===== GCCコアコンパイラ (最小限のGCCコンパイラ) のインストール =====
[https://gcc.gnu.org GCCの公式Webサイト]にアクセスして、GCCのソースコードをダウンロードして解凍する。<br>
または、以下に示すコマンドを実行してダウンロードする。<br>
wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-<バージョン>/gcc-<バージョン>.tar.xz
tar xf gcc-<バージョン>.tar.xz
cd gcc-<バージョン>
<br>
まず、GCCコアコンパイラ (最小限のGCCコンパイラ) を生成する。<br>
このコンパイラは、CRT および winpthreads のビルドに使用する。<br>
mkdir build && cd build
export PATH="/<クロスコンパイラ向けGCCのインストールディレクトリ>/bin:$PATH"
../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-pc-linux-gnu  \
              --target=i686-w64-mingw32    \
              --enable-languages=c,c++    \
              --disable-multilib          \
              --disable-nls                \
              --disable-shared            \
              --disable-werror            \
              --enable-lto
make all-gcc -j $(nproc)
make install-gcc
<br>
===== MinGW-w64 CRTのインストール =====
MinGW-w64のCRT (Cランタイムライブラリ) をビルドおよびインストールする。<br>
cd /<MinGW-w64のソースコードがあるディレクトリ>/mingw-w64-crt
mkdir build-32 && cd build-32
export PATH="/<クロスコンパイラ向けGCCのインストールディレクトリ>/bin:$PATH"
../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ>/i686-w64-mingw32 \
              --build=x86_64-pc-linux-gnu  \
              --host=i686-w64-mingw32      \
              --with-sysroot=<クロスコンパイラ向けGCCのインストールディレクトリ> \
              --enable-lib32              \
              --disable-lib64
make -j $(nproc)
make install
<br>
===== winpthreadsのインストール =====
POSIXスレッドサポートのためのwinpthreadsライブラリをビルドおよびインストールする。<br>
cd /<MinGW-w64のソースコードがあるディレクトリ>/mingw-w64-libraries/winpthreads
mkdir build-32 && cd build-32
export PATH="/<クロスコンパイラ向けGCCのインストールディレクトリ>/bin:$PATH"
../configure --prefix=/<クロスコンパイラ向けGCCのインストールディレクトリ>/i686-w64-mingw32 \
              --build=x86_64-pc-linux-gnu  \
              --host=i686-w64-mingw32      \
              --enable-static              \
              --enable-shared
  make -j $(nproc)
  make -j $(nproc)
  make install
  make install
 
<br>
===== GCCの最終インストール =====
全てのライブラリが揃ったので、完全な機能を持つGCCをビルドおよびインストールする。<br>
cd /<GCCのソースコードがあるディレクトリ>/build
export PATH="<クロスコンパイラ向けGCCのインストールディレクトリ>/bin:$PATH"
../configure --prefix=<クロスコンパイラ向けGCCのインストールディレクトリ> \
              --build=x86_64-pc-linux-gnu  \
              --host=x86_64-pc-linux-gnu  \
              --target=i686-w64-mingw32    \
              --enable-languages=c,c++    \
              --disable-multilib          \
              --disable-nls                \
              --disable-werror            \
              --enable-shared              \
              --enable-threads=posix      \
              --enable-lto                \
              --with-dwarf2
make -j $(nproc)
make install-strip
<br><br>
<br><br>


743行目: 1,112行目:
<br>
<br>
また、Raspberry Pi向けには、Linaro社が提供しているGCC ARMツールチェインを使用することもできる。<br>
また、Raspberry Pi向けには、Linaro社が提供しているGCC ARMツールチェインを使用することもできる。<br>
https://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabihf
* GCCツールチェーン群
* Windows x86 / Linux x86の場合
*: https://snapshots.linaro.org/gnu-toolchain/
*: gcc-linaro-7.5.0-2019.12-i686_arm-linux-gnueabihf.tar.xz
* GCC 10.2 (Bullseye 32ビット向け)
* Linux x64の場合
*: https://snapshots.linaro.org/components/toolchain/binaries/10.2-2021.01-3/arm-linux-gnueabihf/
*: gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf.tar.xz
* GCC 10.2 (Bullseye 64ビット向け)
*: https://snapshots.linaro.org/components/toolchain/binaries/10.2-2021.01-3/aarch64-linux-gnu/
<br>
<u>※注意</u><br>
<u>クロスコンパイラのlibstdc++.so.6ファイルにおいて、Raspberry Pi OSのlibstdc++.so.6ファイルのバージョンと同等または古いものを使用する必要がある。</u><br>
<u>例えば、Raspberry Pi OS Bullseyeのlibstdc++.so.6ファイルはGLIBCXX_3.4.28までであるため、GCC 10ツールチェーン、または、それ以前のものを使用する。</u><br>
<u>また、Raspberry Pi OS Bookwormのlibstdc++.so.6ファイルはGLIBCXX_3.4.30までであるため、GCC 12ツールチェーン、または、それ以前のものを使用する。</u><br>
<br>
<br>


758行目: 1,133行目:
<code>ac_cv_func_malloc_0_nonnull=yes ac_cv_func_realloc_0_nonnull=yes</code>オプションを付加する。<br>
<code>ac_cv_func_malloc_0_nonnull=yes ac_cv_func_realloc_0_nonnull=yes</code>オプションを付加する。<br>
これは、autotoolsの仕様で、GLIBC以外のシステム(例. uClibc等)を使用しているからである。<br>
これは、autotoolsの仕様で、GLIBC以外のシステム(例. uClibc等)を使用しているからである。<br>
  # 32bit Cortex-A(Hard Float版)向けにビルドする場合
  # 32bit Cortex-A (Hard Float) 向けにビルドする場合
  ac_cv_func_malloc_0_nonnull=yes ac_cv_func_realloc_0_nonnull=yes \
  ac_cv_func_malloc_0_nonnull=yes \
  ../configure --prefix=<ソフトウェアのインストールディレクトリ> \
ac_cv_func_realloc_0_nonnull=yes \
CC=/<GCC_ARMのインストールディレクトリ>/bin/arm-none-linux-gnueabihf-gcc \
  ../configure --prefix=<ソフトウェアのインストールディレクトリ>                             \
CXX=/<GCC_ARMのインストールディレクトリ>/bin/arm-none-linux-gnueabihf-g++ \
              CC=/<GCC_ARMのインストールディレクトリ>/bin/arm-none-linux-gnueabihf-gcc \
--build=x86_64-unknown-linux-gnu --host=arm-none-linux-gnueabihf --target=arm-none-linux-gnueabihf \
              CXX=/<GCC_ARMのインストールディレクトリ>/bin/arm-none-linux-gnueabihf-g++ \
--with-sysroot=/<GCC_ARMのインストールディレクトリ>/arm-none-linux-gnueabihf/libc --with-gnu-ld
              --build=x86_64-unknown-linux-gnu                                 \
              --host=arm-none-linux-gnueabihf                                   \
              --target=arm-none-linux-gnueabihf                                 \
              --with-sysroot=/<GCC_ARMのインストールディレクトリ>/arm-none-linux-gnueabihf/libc \
              --with-gnu-ld
   
   
  make -j $(nproc) LDFLAGS="-L/<GCC_ARMのインストールディレクトリ>/arm-none-linux-gnueabihf/lib" \
  make -j $(nproc) LDFLAGS="-L/<GCC_ARMのインストールディレクトリ>/arm-none-linux-gnueabihf/lib" \
CFLAGS="-I/<GCC_ARMのインストールディレクトリ>/arm-none-linux-gnueabihf/include"
                  CFLAGS="-I/<GCC_ARMのインストールディレクトリ>/arm-none-linux-gnueabihf/include"
  make install
  make install
<br>
<br>
  # 64bit Cortex-A向けにビルドする場合
  # 64bit Cortex-A向けにビルドする場合
  ac_cv_func_malloc_0_nonnull=yes ac_cv_func_realloc_0_nonnull=yes \
  ac_cv_func_malloc_0_nonnull=yes \
  ../configure --prefix=<ソフトウェアのインストールディレクトリ> \
ac_cv_func_realloc_0_nonnull=yes \
CC=/<GCC_ARMのインストールディレクトリ>/bin/aarch64-none-linux-gnu-gcc \
  ../configure --prefix=<ソフトウェアのインストールディレクトリ>                           \
CXX=/<GCC_ARMのインストールディレクトリ>/bin/aarch64-none-linux-gnu-g++ \  
              CC=/<GCC_ARMのインストールディレクトリ>/bin/aarch64-none-linux-gnu-gcc \
--build=x86_64-unknown-linux-gnu --host=aarch64-none-linux-gnu --target=aarch64-none-linux-gnu \
              CXX=/<GCC_ARMのインストールディレクトリ>/bin/aarch64-none-linux-gnu-g++ \  
--with-sysroot=/<GCC_ARMのインストールディレクトリ>/aarch64-none-linux-gnu/libc --with-gnu-ld
              --build=x86_64-unknown-linux-gnu                               \
              --host=aarch64-none-linux-gnu                                   \
              --target=aarch64-none-linux-gnu                                 \
              --with-sysroot=/<GCC_ARMのインストールディレクトリ>/aarch64-none-linux-gnu/libc \
              --with-gnu-ld
   
   
  make -j $(nproc) LDFLAGS="-L/<GCC_ARMのインストールディレクトリ>/aarch64-none-linux-gnu/lib64" \
  make -j $(nproc) LDFLAGS="-L/<GCC_ARMのインストールディレクトリ>/aarch64-none-linux-gnu/lib64" \
CFLAGS="-I/<GCC_ARMのインストールディレクトリ>/aarch64-none-linux-gnu/include"
                  CFLAGS="-I/<GCC_ARMのインストールディレクトリ>/aarch64-none-linux-gnu/include"
  make install
  make install
<br>
<br>
787行目: 1,168行目:
以下の例では、SourceForgeからダウンロードした<u>Raspberry Pi専用のGCC ARMツールチェーン</u>を使用して、<br>
以下の例では、SourceForgeからダウンロードした<u>Raspberry Pi専用のGCC ARMツールチェーン</u>を使用して、<br>
任意のソフトウェアをRaspberry Pi向けにビルドしている。<br>
任意のソフトウェアをRaspberry Pi向けにビルドしている。<br>
  ac_cv_func_malloc_0_nonnull=yes ac_cv_func_realloc_0_nonnull=yes \
# Raspberry Pi OS 32ビットの場合
  ../configure --prefix=<ソフトウェアのインストールディレクトリ> \
  ac_cv_func_malloc_0_nonnull=yes \
CC=/<GCC_ARMのインストールディレクトリ>/bin/arm-linux-gnueabihf-gcc CXX=/<GCC_ARMのインストールディレクトリ>/bin/arm-linux-gnueabihf-g++ \
ac_cv_func_realloc_0_nonnull=yes \
--build=x86_64-unknown-linux-gnu --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
  ../configure --prefix=<ソフトウェアのインストールディレクトリ>                       \
              CC=/<GCC_ARMのインストールディレクトリ>/bin/arm-linux-gnueabihf-gcc \
              CXX=/<GCC_ARMのインストールディレクトリ>/bin/arm-linux-gnueabihf-g++ \
              --build=x86_64-unknown-linux-gnu \
              --host=arm-linux-gnueabihf       \
              --target=arm-linux-gnueabihf
   
   
  make -j $(nproc)
  make -j $(nproc)
make install
<br>
# Raspberry Pi OS 64ビットの場合
../configure --prefix=<ソフトウェアのインストールディレクトリ>                      \
              CC=/<GCC_ARMのインストールディレクトリ>/bin/aarch64-linux-gnu-gcc  \
              CXX=/<GCC_ARMのインストールディレクトリ>/bin/aarch64-linux-gnu-g++ \
              --build=x86_64-unknown-linux-gnu \
              --host=aarch64-linux-gnu        \
              --target=aarch64-linux-gnu
   
   
make -j $(nproc)
  make install
  make install
<br><br>
== PIE(Position Independent Executable)==
PIEは、実行時にメモリ上の任意のアドレスに配置できる実行可能ファイルの形式である。<br>
<br>
==== PIEの概念 ====
通常の実行ファイルは、コンパイル時に固定されたメモリアドレスを前提として生成される。<br>
例えば、プログラムのコードセクションは常に 0x400000 番地から始まる、といった具合である。<br>
<br>
一方、PIEでコンパイルされた実行ファイルは、相対アドレス指定を使用するため、メモリ上のどこに配置されても正常に動作する。<br>
プログラム内の関数呼び出しやデータアクセスは、絶対アドレスではなく、現在位置からの相対的なオフセットで計算される。<br>
<br>
PIEのメリットは、ASLR (Address Space Layout Randomization) と組み合わせることにより、強力なセキュリティ対策になることである。<br>
<br>
ASLRは、プログラム実行時にコードやデータをメモリ上のランダムな位置に配置する技術である。<br>
PIEでない実行ファイルは固定アドレスに配置されるため、攻撃者が脆弱性を悪用する際にジャンプ先のアドレスを予測しやすくなる。<br>
PIEを使用することにより、実行するたびにプログラムの配置位置が変わるため、バッファオーバーフロー や ROP (Return-Oriented Programming) 攻撃等が困難になる。<br>
<br>
PIEは実行ファイル向けの技術であるが、似た概念として共有ライブラリ向けのPIC (Position Independent Code) がある。<br>
<br>
デフォルトでPIEが有効になっていないGCCでPIEを使用する場合は、コンパイル時とリンク時の両方でオプションを指定する。<br>
# コンパイル時
gcc -fPIE -c program.c
# リンク時
gcc -pie -o program program.o
<br>
Ubuntu 17.10以降、Debian 9以降、Fedora 23以降等、多くのLinuxディストリビューションでは、PIEがデフォルトで有効になっている。<br>
<br><br>
<br><br>