从网上看到一篇编译gcc的文章,自己动手试了下,改了些代码,成功。下面是具体步骤:
1. 下载二进制文件处理工具集合binutils-2.20.1.tar.bz2,下载地址为 http://ftp.gnu.org/gnu/binutils/binutils-2.20.1.tar.bz2
2. 下载GNU编译器集合gcc-4.5.1.tar.bz2(也可仅下载core和g++),下载地址为 http://gcc.parentingamerica.com/releases/gcc-4.5.1/gcc-4.5.1.tar.bz2
3. 下载编译GCC-4.5.0的依赖库:
gmp-5.0.1.tar.bz2: 下载地址为 ftp://ftp.gmplib.org/pub/gmp-5.0.1/gmp-5.0.1.tar.bz2
mpfr-3.0.0.tar.bz2: 下载地址为 http://www.mpfr.org/mpfr-current/mpfr-3.0.0.tar.bz2
mpc-0.8.2.tar.gz: 下载地址为 http://www.multiprecision.org/mpc/download/mpc-0.8.2.tar.gz
4. 下载C语言库newlib-1.18.0.tar.gz,下载地址为 ftp://sources.redhat.com/pub/newlib/newlib-1.18.0.tar.gz
5. 下载调试器gdb-7.2.tar.bz2,下载地址为 http://ftp.gnu.org/gnu/gdb/gdb-7.2.tar.bz2
6. 将所有文件都拷贝到同一目录下,然后进入此目录,执行:gedit Makefile &
如果仅是编译内核或其他不进行系统调用的程序,可以添加以下内容:
TARGET= arm-cortex-eabi
PREFIX=/opt/$(TARGET)-tools/
TEMP_PREFIX=$(HOME)/build-temp/
PROCS=2
BINUTILS_REV = 2.20.1
GCC_REV = 4.5.1
GMP_REV = 5.0.1
MPFR_REV = 3.0.0
MPC_REV = 0.8.2
NEWLIB_REV = 1.18.0
NEWLIB_FLAGS="-ffunction-sections -fdata-sections -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -Os -fomit-frame-pointer -fno
-unroll-loops -D__BUFSIZ__=256 -mabi=aapcs"
GDB_REV = 7.2
all: prerequest install-deps install-cross
prerequest:
mkdir -p $(PREFIX)/bin
mkdir -p $(TEMP_PREFIX)/lib
apt-get install curl flex bison libgmp3-dev libmpfr-dev autoconf texinfo build-essential texlive libncurses5-dev
install-deps: gmp mpfr mpc
install-cross: cross-binutils cross-gcc cross-g++ cross-newlib cross-gdb
gmp: gmp-$(GMP_REV).tar.bz2
tar -xvf $<
mkdir -p build/gmp && cd build/gmp && /
pushd ../../gmp-* ; /
make clean; /
popd ; /
../../gmp-*/configure --disable-shared --prefix=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
mpfr: mpfr-$(MPFR_REV).tar.bz2
tar -xvf $<
mkdir -p build/mpfr && cd build/mpfr && /
bushed ../../mpfr-* ; /
make clean ; /
popd ; /
../../mpfr-*/configure LDFLAGS="-Wl,-search_paths_first" --disable-shared --with-gmp=$(TEMP_PREFIX) --
prefix=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) all && /
$(MAKE) install
mpc: mpc-$(MPC_REV).tar.gz gmp mpfr
tar -xvf $<
mkdir -p build/mpc && cd build/mpc && /
pushed ../../mpc-* ; /
make clean ; /
popd ; /
../../mpc-*/configure --disable-shared --with-gmp=$(TEMP_PREFIX) --with-mpfr=$(TEMP_PREFIX) --prefix=$(TEMP_PREFIX)
&& /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
cross-binutils: binutils-$(BINUTILS_REV).tar.bz2
tar -xvf $<
mkdir -p build/binutils && cd build/binutils && /
pushd ../../binutils-* ; /
make clean ; /
popd ; /
../../binutils-*/configure --prefix=$(PREFIX) --target=$(TARGET) --disable-shared --disable-nls --disable-werror && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
cross-gcc: gcc-$(GCC_REV).tar.bz2 cross-binutils
tar -xvf $<
mkdir -p build/gcc && cd build/gcc && /
pushd ../../gcc-* ; /
make clean ; /
popd ; /
export LD_LIBRARY_PATH=$(TEMP_PREFIX)/lib:$(LD_LIBRARY_PATH) && /
../../gcc-*/configure --prefix=$(PREFIX) --target=$(TARGET) --enable-languages="c" --with-gnu-ld --with-gnu-as --
with-newlib --disable-nls --disable-libssp --with-newlib --without-headers --disable-shared --disable-threads --disable-
libmudflap --disable-libgomp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-libffi --enable-extra-sgxxlite
-multilibs --with-gmp=$(TEMP_PREFIX) --with-mpfr=$(TEMP_PREFIX) --with-mpc=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install && /
ln -sv $(PREFIX)/bin/$(TARGET)-gcc $(PREFIX)/bin/$(TARGET)-cc
cross-newlib: newlib-$(NEWLIB_REV).tar.gz cross-binutils cross-gcc
tar -xvf $<
mkdir -p build/newlib && cd build/newlib && /
pushd ../../newlib-* ; /
make clean ; /
popd ; /
export PATH=$(PREFIX)/bin:$(PATH) && /
export LD_LIBRARY_PATH=$(TEMP_PREFIX)/lib:$(LD_LIBRARY_PATH) && /
../../newlib-*/configure --prefix=$(PREFIX) --target=$(TARGET) --disable-newlib-supplied-syscalls --disable-libgloss
--disable-nls --disable-shared && /
$(MAKE) -j$(PROCS) CFLAGS_FOR_TARGET=$(NEWLIB_FLAGS) CCASFLAGS=$(NEWLIB_FLAGS) && /
$(MAKE) install
cross-g++: gcc-$(GCC_REV).tar.bz2 cross-gcc cross-newlib
tar -xvf $<
mkdir -p build/g++ && cd build/g++ && /
pushd ../../gcc-* ; /
make clean ; /
popd ; /
export PATH=$(PREFIX)/bin:$(PATH) && /
export LD_LIBRARY_PATH=$(TEMP_PREFIX)/lib:$(LD_LIBRARY_PATH) && /
../../gcc-*/configure --prefix=$(PREFIX) --target=$(TARGET) --enable-languages="c++" --with-gnu-ld --with-gnu-as --
with-newlib --disable-nls --disable-libssp --with-newlib --without-headers --disable-shared --disable-threads --disable-
libmudflap --disable-libgomp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-libffi --enable-extra-sgxxlite
-multilibs --with-gmp=$(TEMP_PREFIX) --with-mpfr=$(TEMP_PREFIX) --with-mpc=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
cross-gdb: gdb-$(GDB_REV).tar.bz2
tar -xvf $<
mkdir -p build/gdb && cd build/gdb && /
pushd ../../gdb-* ;/
make clean ; /
popd ; /
export PATH=$(PREFIX)/bin:$(PATH) && /
export LD_LIBRARY_PATH=$(TEMP_PREFIX)/lib:$(LD_LIBRARY_PATH) && /
../../gdb-*/configure --prefix=$(PREFIX) --target=$(TARGET) --disable-shared --disable-werror && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
.PHONY: clean, distclean
distclean: clean
rm -fr $(PREFIX)
clean:
rm -fr build binutils-$(BINUTILS_REV) gcc-$(GCC_REV) gdb-$(GDB_REV) gmp-$(GMP_REV) mpc-$(MPC_REV) mpfr-$(MPFR_REV)
newlib-$(NEWLIB_REV) $(TEMP_PREFIX)
如果想完整编译,添加以下内容:
TARGET= arm-linux-eabi
PREFIX=/opt/arm-linux-tools/
TEMP_PREFIX=$(HOME)/build-temp/
PROCS=2
BINUTILS_REV = 2.20.1
GCC_REV = 4.5.1
GMP_REV = 5.0.1
MPFR_REV = 3.0.0
MPC_REV = 0.8.2
NEWLIB_REV = 1.18.0
NEWLIB_FLAGS="-ffunction-sections -fdata-sections -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -Os -fomit-frame-pointer -fno-unroll-loops -D__BUFSIZ__=256 -mabi=aapcs"
GDB_REV = 7.2
all: prerequest install-deps install-cross
prerequest:
mkdir -p $(PREFIX)/bin
mkdir -p $(TEMP_PREFIX)/lib
apt-get install curl flex bison libgmp3-dev libmpfr-dev autoconf texinfo build-essential texlive libncurses5-dev
install-deps: gmp mpfr mpc
install-cross: cross-binutils cross-newlib cross-gcc1 cross-gcc2 cross-gdb
gmp: gmp-$(GMP_REV).tar.bz2
tar -xvf $<
mkdir -p build/gmp && cd build/gmp && /
pushd ../../gmp-* ; /
make clean; /
popd ; /
../../gmp-*/configure --disable-shared --prefix=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
mpfr: mpfr-$(MPFR_REV).tar.bz2 gmp
tar -xvf $<
mkdir -p build/mpfr && cd build/mpfr && /
bushed ../../mpfr-* ; /
make clean ; /
popd ; /
../../mpfr-*/configure LDFLAGS="-Wl,-search_paths_first" --disable-shared --with-gmp=$(TEMP_PREFIX) --prefix=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) all && /
$(MAKE) install
mpc: mpc-$(MPC_REV).tar.gz gmp mpfr
tar -xvf $<
mkdir -p build/mpc && cd build/mpc && /
pushed ../../mpc-* ; /
make clean ; /
popd ; /
../../mpc-*/configure --disable-shared --with-gmp=$(TEMP_PREFIX) --with-mpfr=$(TEMP_PREFIX) --prefix=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
cross-binutils: binutils-$(BINUTILS_REV).tar.bz2
tar -xvf $<
mkdir -p build/binutils && cd build/binutils && /
pushd ../../binutils-* ; /
make clean ; /
popd ; /
../../binutils-*/configure --prefix=$(PREFIX) --target=$(TARGET) --disable-shared --disable-nls --disable-werror && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
cross-gcc1: gcc-$(GCC_REV).tar.bz2 cross-binutils
tar -xvf $<
mkdir -p build/gcc && cd build/gcc && /
pushd ../../gcc-* ; /
make clean ; /
popd ; /
export LD_LIBRARY_PATH=$(TEMP_PREFIX)/lib:$(LD_LIBRARY_PATH) && /
../../gcc-*/configure --prefix=$(PREFIX) --target=$(TARGET) --enable-languages="c" --with-gnu-ld --with-gnu-as --with-newlib --disable-nls --disable-shared --disable-threads --disable-libssp --disable-libmudflap --with-gmp=$(TEMP_PREFIX) --with-mpfr=$(TEMP_PREFIX) --with-mpc=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install && /
ln -sv $(PREFIX)/bin/$(TARGET)-gcc $(PREFIX)/bin/$(TARGET)-cc
cross-newlib: newlib-$(NEWLIB_REV).tar.gz cross-binutils cross-gcc1
tar -xvf $<
mkdir -p build/newlib && cd build/newlib && /
pushd ../../newlib-* ; /
make clean ; /
popd ; /
export PATH=$(PREFIX)/bin:$(PATH) && /
export LD_LIBRARY_PATH=$(TEMP_PREFIX)/lib:$(LD_LIBRARY_PATH) && /
../../newlib-*/configure --prefix=$(PREFIX) --target=$(TARGET) --disable-nls --disable-shared && /
$(MAKE) -j$(PROCS) CFLAGS_FOR_TARGET=$(NEWLIB_FLAGS) CCASFLAGS=$(NEWLIB_FLAGS) && /
$(MAKE) install
cross-gcc2: gcc-$(GCC_REV).tar.bz2 cross-gcc1 cross-newlib
tar -xvf $<
mkdir -p build/g++ && cd build/g++ && /
pushd ../../gcc-* ; /
make clean ; /
popd ; /
export PATH=$(PREFIX)/bin:$(PATH) && /
export LD_LIBRARY_PATH=$(TEMP_PREFIX)/lib:$(LD_LIBRARY_PATH) && /
../../gcc-*/configure --prefix=$(PREFIX) --target=$(TARGET) --enable-languages="c,c++" --with-gnu-ld --with-gnu-as --with-newlib --disable-nls --disable-shared --with-gmp=$(TEMP_PREFIX) --with-mpfr=$(TEMP_PREFIX) --with-mpc=$(TEMP_PREFIX) && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
cross-gdb: gdb-$(GDB_REV).tar.bz2 cross-gcc2
tar -xvf $<
mkdir -p build/gdb && cd build/gdb && /
pushd ../../gdb-* ;/
make clean ; /
popd ; /
export PATH=$(PREFIX)/bin:$(PATH) && /
export LD_LIBRARY_PATH=$(TEMP_PREFIX)/lib:$(LD_LIBRARY_PATH) && /
../../gdb-*/configure --prefix=$(PREFIX) --target=$(TARGET) --disable-shared --disable-werror && /
$(MAKE) -j$(PROCS) && /
$(MAKE) install
.PHONY: clean, distclean
distclean: clean
rm -fr $(PREFIX)
clean:
rm -fr build binutils-$(BINUTILS_REV) gcc-$(GCC_REV) gdb-$(GDB_REV) gmp-$(GMP_REV) mpc-$(MPC_REV) mpfr-$(MPFR_REV) newlib-$(NEWLIB_REV) $(TEMP_PREFIX)
7. gedit ~/.bashrc &
在文件末尾添加以下内容:
export PATH=/opt/arm-cortex-eabi-tools/bin:$(PATH)
8. sudo make all
9. sudo make clean
至此,工具编译成功。对于第一种配置,不产生crt0.o,并且工具链不提供任何系统调用支持,所以不能编译应用程序,仅能编译内核或者其他与不进行任何系统调用的程序,目标系统试过arm-elf,也能成功编译。对于第二种配置可以编译内核和应用程序,试过arm-linux-eabi,都可以成功编译。