18
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GCCコンパイル方法

Last updated at Posted at 2015-05-06

GCCをコンパイルする方法

C,C++のコードのコンパイルにお馴染みのGCCのコンパイル方法を使用します。
最近だとパッケージマネージャとか、binをダウンロードしてコンパイルされたものが手に入ってしまう便利な世の中ですが、DIYが大好きな人とかクロスコンパイラが欲しい人に役立てば幸せかな?

ファイル集め

必要なファイルを集めませう。

まずGCCのコンパイルに必要なbin-utils。アセンブラもぞもぞするのに必要です。
binutilsのftpサイト ftp://ftp.gnu.org/gnu/binutils/
コンソールを使ってダウンしませう

wget ftp://ftp.gnu.org/gnu/binutils/binutils-VERSION.tar.gz

VERSIONの所は自分で変えてね。

newlibのソース。newlibは標準Cライブラリでgccの中に組み込まれます。
newlibのftpサイト ftp://sourceware.org/pub/newlib/
wget ftp://sourceware.org/pub/newlib/newlib-VERSION.tar.gz

次にTARGETのGCCのソース
GCCのftpサイト ftp://ftp.gnu.org/gnu/gcc/
これもwgetを使えばコンソールからダウンできます。

wget ftp://ftp.gnu.org/gnu/gcc/gcc-VERSION/gcc-VERSION.tar.gz

そして、最後にコンパイル対象のOSのヘッダファイル。
ここではlinux向けのGCCを作るのでlinuxのソースをダウンします。
Linuxのftpサイト ftp://ftp.kernel.org/pub/linux/kernel/
これもwgetを使えばコンソールからダウンできます。

wget ftp://ftp.kernel.org/pub/linux/kernel/v3.x/linux-VERSION.tar.gz

下準備

これから集めたファイルを使ってゴリゴリmakeしていくのですが、やりやすいように下準備をしていきます。
ダウンしたファイルは圧縮・アーカイブ化されているのでtarコマンドで解凍します。

tar xvzf binutils-VERSION.tar.gz
tar xvzf gcc-VERSION.tar.gz
tar xvzf linux-VERSION.tar.gz
tar xvzf newlib-VERSION.tar.gz

*.xzのファイルをダウンしてきた場合はtar -xvJf hogehoge.tar.xzを使って解凍してください。

作ったものを入れるディレクトリを作ります。

mkdir build-binutils build-gcc build-newlib
mkdir /opt/cross-gcc/

ARCHはi386やx86などビルド対象のアーキテクチャを入れてください。
linuxのヘッダファイルを作ります。
解凍したlinux-VERSION/以下で

make ARCH=i386 CROSS_COMPILE=i386-linux menuconfig

を実行します。必要に応じてカーネルパラメータをいじりませう。
これをやるとgccをビルドするときに必要なlinuxヘッダが出てきます。
これを出力先のディレクトリにコピーしておきます。

mkdir -p /opt/cross-gcc/include
cp -r include/linux /opt/cross-gcc/include
cp -r include/asm-i386 /opt/cross-gcc/include/asm
cp -r include/asm-generic /opt/cross-gcc/include/

gccのビルドに必要なmpc、mpfr、gmpなどを取り込みます。
誰かが作ってくれたいいスクリプトがgcc-VERSION/contrib/download_prerequisitesが自動的にやってくれます。

cd gcc-VERSION/
./contrib/download_prerequisites

準備完了!!

binutilsのビルド

cd build-binutils
../binutils-VERSION/configure \
--target=i386-linux-gnu --host=x86_64-linux \
--enable-languages=c,c++ \
--prefix=/opt/cross-gcc --disable-multilib -v
make -j20 all

gccのビルド(一回目)

cd build-gcc
../gcc-VERSION/configure \
--target=i386-linux --prefix=/opt/cross-gcc \
--host=x86_64-linux-gnu --without-headers \
--enable-languages=c,c++ \
--with-newlib --disable-threads -v
make -j20 all-gcc
make install-gcc

new libのビルド

一回目で出てきたgccを使ってnewlibをビルドする。

cd build-newlib
../newlib-VERSION/configure
--target=i386-linux --host=x86_64-linux-gnu \
--prefix=/opt/cross-gcc
make
sudo make install

gccのビルド(二回目)

cd build-gcc
rm -rf *
../gcc-VERSION/configure --enable-languages=c,c++ 
--target=i386-linux --host=x86_64-linux-gnu \
--prefix=/opt/cross-gcc
make all -j20
make install

gccのテスト

とりあえずハローワールドをコンパイル。

hello.cpp
# include <iostream>
# include <cstdlib>
using namespace std;
int main()
{
    cout << "Hello World!" << endl;
    return EXIT_SUCCESS;
}

これをコンパイルして実効。

i386-linx-gnu-g++ -o test hello.cpp
./hello.cpp

使い方

makeと一緒に使う場合は、

make CC=i386-linux-gnu-gcc CXX=i386-linux-gnu-g++

gnuのconfigureスクリプトと一緒に使う場合は

CC=i386-linux-gnu-gcc ./configure

でいけます。
HAPPY HACKING

18
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?