編譯gcc-4.6.3
準備
下載GCC 源碼包 : gcc-4.6.3.tar.bz2
下載GCC 依賴包: gmp-5.0.4.tar.bz2, mpfr-3.1.0.tar.bz2 ,mpc-0.9.tar.gz
解壓gcc-4.6.3.tar.bz2 指令=》 [flydream@flydream opt]$ tar -xvf gcc-4.6.3.tar.bz2
進入[flydream@flydream opt]$ cd gcc-4.6.3
把下載的gmp-5.0.4.tar.bz2, mpfr-3.1.0.tar.bz2 ,mpc-0.9.tar.gz包放到gcc-4.6.3目錄
在gcc-4.6.3目錄下分別解壓上面的三個包
編譯依賴
先安裝gmp-5.0.4.tar.bz2
# 進入gmp目錄:
$ cd gmp-5.0.4
# 建立安裝路徑:
$ mkdir gmp_install
$ cd gmp_install
$ ../configure --prefix=/opt/gcc-4.6.3/gmp-5.0.4/gmp_install
$ make
$ make install
安裝mpfr-3.1.0.tar.bz2
# 進入mpfr目錄
$ cd mpfr-3.1.0
# 建立安裝路徑: $
$ mkdir mpfr_install
$ cd mpfr_install
$ ../configure --prefix=/opt/gcc-4.6.3/mpfr-3.1.0/mpfr_install --with-gmp=/opt/gcc-4.6.3/gmp-5.0.4/gmp_install
$ make
$ make install
然后安裝mpc-0.9.tar.gz
# 進入mpc
# 建立安裝路徑:
$ mkdir mpc_install
cd mpc_install
$ ../configure --prefix=/opt/gcc-4.6.3/mpc-0.9/mpc_install --with-gmp=/opt/gcc-4.6.3/gmp-5.0.4/gmp_install --with-mpfr=/opt/gcc-4.6.3/mpfr-3.1.0/mpfr_install
$ make
$ make install
在編譯GCC的過程中可能出現“configure: error: cannot compute suffix of object files: cannot compile”的錯誤,解決方法是:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/gcc-4.6.3/mpc-0.9/mpc_install/lib:/opt/gcc-4.6.3/gmp-5.0.4/gmp_install/lib:/opt/gcc-4.6.3/mpfr-3.1.0/mpfr_install/lib
最后編譯安裝GCC
在GCC源碼目錄中建立安裝路徑
$ mkdir gcc_install
$ cd gcc_install
$ ../configure --prefix=/opt/gcc_install --with-gmp=/opt/gcc-4.6.3/gmp-5.0.4/gmp_install --with-mpfr=/opt/gcc-4.6.3/mpfr-3.1.0/mpfr_install ----with-mpc=/opt/gcc-4.6.3/mpc-0.9/mpc_install --enable-checking=release --program-suffix=4.6.3 --enable-languages=c,c++
$ make
$ make install
注意–program-suffix參數,表示生成的可執行文件的后綴。–enable-languages參數表示要支持的語言。最后make; make install即可。make的時候還有個小技巧:因為gcc文件很多,編譯很慢,可以使用make -j N參數,開啟多線程編輯。其中N值可以設定為機器CPU核數x2。
編譯好了之后就可以使用/opt/gcc-4.6.3/bin/gcc-4.6.3來編譯c程序了。為了使用方便,可以將/opt/gcc-4.6.3/bin/gcc-4.6.3/bin放到系統PATH中:
export PATH=$PATH:/opt/gcc-4.6.3/bin/gcc-4.6.3/bin
構建arm-linux-gnueabi-gcc-4.6.3交叉編譯鏈
準備工作
1、準備文件
binutils-2.22.tar.bz2
gcc-4.6.3.tar.bz2
glibc-2.14.1.tar.xz
glibc-ports-2.14.1.tar.gz
glibc-linuxthreads-2.5.tar.bz2
gdb-7.4.1.tar.bz2
Linux-2.6.38.8.tar.bz2
mpfr-3.1.1.tar.bz2
mpc-1.0.1.tar.gz
gmp-5.1.0.tar.bz2
2、建立以下變量,方便稍候的輸入
$ vim ~/.bashrc
export TARGET=arm-linux-gnueabi
export PREFIX=/usr/local/arm-linux-gcc/4.6.3
export TARGET_PREFIX=$PREFIX/$TARGET
export PATH=$PATH:$PREFIX/bin
3、為宿主機安裝mpfr、mpc、gmp
$ tar -xvf gmp-5.1.0.tar.bz2
$ cd gmp-5.1.0
$ mkdir build
$ cd build
$ ../configure
$ make all -j4
$ make install
$ tar -xvf mpfr-3.1.1.tar.bz2
$ cd mpfr-3.1.1
$ mkdir build
$ cd build
$ ../configure
$ make all -j4
$ make install
$ tar -xvf mpc-1.0.1.tar.gz
$ cd mpc-1.0.1
$ mkdir build
$ cd build
$ ../configure
$ make all -j4
$ make install
4、復制linux-kernel-headers
$ tar -xvf linux-2.6.38.8.tar.bz2
$ cd linux-2.6.38.8
$ make include/linux/version.h
# CHK include/linux/version.h
# UPD include/linux/version.h
$ mkdir -p $TARGET_PREFIX/include
$ cp -r ./include/linux/ $TARGET_PREFIX/include
$ cp -r ./include/asm-generic/ $TARGET_PREFIX/include
$ cp -r ./arch/arm/include/asm/ $TARGET_PREFIX/include
二、編譯binutils
$ tar -xvf binutils-2.22.tar.bz2
$ cd binutils-2.22
$ mkdir build
$ cd build
$ ../configure --prefix=$PREFIX --target=$TARGET
$ make all -j4
$ make install
三、初步編譯gcc(不帶glibc支持)
$ tar -xvf gcc-4.6.3.tar.bz2
重新解壓mpfr-3.1.1.tar.bz2、mpc-1.0.1.tar.gz、gmp-5.1.0.tar.bz2,并改名復制至gcc-4.6.3中
$ rm -rf mpfr-3.1.1 mpc-1.0.1 gmp-5.1.0
$ tar -xvf gmp-5.1.0.tar.bz2
$ mv gmp-5.1.0 gmp
$ mv ./gmp ./gcc-4.6.3
$ tar -xvf mpfr-3.1.1.tar.bz2
$ mv mpfr-3.1.1 mpfr
$ mv ./mpfr ./gcc-4.6.3
$ tar -xvf mpc-1.0.1.tar.gz
$ mv mpc-1.0.1 mpc
$ mv ./mpc ./gcc-4.6.3
$ cd gcc-4.6.3
$ mkdir build
$ cd build
$ ../configure --prefix=$PREFIX --target=$TARGET --without-headers --enable-languages=c --disable-threads --with-newlib --disable-shared --disable-libmudflap --disable-libssp --disable-decimal-float
$ make all-gcc -j4
$ make install-gcc
$ make all-target-libgcc -j4
$ make install-target-libgcc
四、編譯glibc
$ tar -xvf glibc-2.14.1.tar.xz
$ tar -xvf glibc-ports-2.14.1.tar.gz
$ mv glibc-ports-2.14.1 ports
$ mv ./ports/ ./glibc-2.14.1
$ tar -xvf glibc-linuxthreads-2.5.tar.bz2 --directory=./glibc-2.14.1
$ cd glibc-2.14.1
$ mkdir build
$ cd build
$ CC=$TARGET-gcc
$ ln -s /usr/local/arm-linux-gcc/4.6.3/lib/gcc/arm-linux-gnueabi/4.6.3/libgcc.a /usr/local/arm-linux-gcc/4.6.3/lib/gcc/arm-linux-gnueabi/4.6.3/libgcc_eh.a
創建config.cache配置文件
$ vim config.cache
libc_cv_forced_unwind=yes
libc_cv_c_cleanup=yes
libc_cv_arm_tls=yes
$ ../configure --host=$TARGET --target=$TARGET --prefix=$TARGET_PREFIX --enable-add-ons --disable-profile --cache-file=config.cache --with-binutils=$PREFIX/bin --with-headers=$TARGET_PREFIX/include
$ make all -j4
$ make install
五、重新編譯gcc
$ cd gcc-4.6.3/build/
$ rm -rf *
$ ../configure --prefix=$PREFIX --target=$TARGET --enable-shared --enable-languages=c,c++
$ make all -j4
$ make install
六、編譯gdb
$ tar -xvf gdb-7.4.1.tar.bz2
$ ../configure --prefix=$PREFIX --target=$TARGET
$ make all -j4
$ make install
七、創建鏈接
$ cd $PREFIX/bin
$ ln -s arm-linux-gnueabi-addr2line arm-linux-addr2line
$ ln -s arm-linux-gnueabi-ar arm-linux-ar
$ ln -s arm-linux-gnueabi-as arm-linux-as
$ ln -s arm-linux-gnueabi-c++ arm-linux-c++
$ ln -s arm-linux-gnueabi-C++filt arm-linux-c++filt
$ ln -s arm-linux-gnueabi-cpp arm-linux-cpp
$ ln -s arm-linux-gnueabi-elfedit arm-linux-elfedit
$ ln -s arm-linux-gnueabi-g++ arm-linux-g++
$ ln -s arm-linux-gnueabi-gcc arm-linux-gcc
$ ln -s arm-linux-gnueabi-gcc-4.6.3 arm-linux-gcc-4.6.3
$ ln -s arm-linux-gnueabi-gcov arm-linux-gcov
$ ln -s arm-linux-gnueabi-gdb arm-linux-gdb
$ ln -s arm-linux-gnueabi-gdbtui arm-linux-gdbtui
$ ln -s arm-linux-gnueabi-gprof arm-linux-gprof
$ ln -s arm-linux-gnueabi-ld arm-linux-ld
$ ln -s arm-linux-gnueabi-ld.bfd arm-linux-ld.bfd
$ ln -s arm-linux-gnueabi-nm arm-linux-nm
$ ln -s arm-linux-gnueabi-objcopy arm-linux-objcopy
$ ln -s arm-linux-gnueabi-objdump arm-linux-objdump
$ ln -s arm-linux-gnueabi-ranlib arm-linux-ranlib
$ ln -s arm-linux-gnueabi-readelf arm-linux-readelf
$ ln -s arm-linux-gnueabi-run arm-linux-run
$ ln -s arm-linux-gnueabi-size arm-linux-size
$ ln -s arm-linux-gnueabi-strings arm-linux-strings
$ ln -s arm-linux-gnueabi-strip arm-linux-strip
最近想用gcc來dump一些信息出來,比如說文件依賴關系,ast。google了一下,有兩種方法:hack跟plugin。codeviz是通過修改gcc3.4.6來dump出函數調用關系,而VCG是gcc的一個插件,用來可以用來分析控制流圖,函數調用圖等諸多信息。但gcc是到4.5之后才支持plugin的機制,我機器版本太低,下了gcc4.7.1(各版本的地址http://ftp.gnu.org/gnu/gcc/),根據官方建議,新建一個目錄用于編譯,因gcc的make里面,沒有clean,不過對我們編譯其他開源項目同樣有用:ls的時候比較干凈:)。
下載依賴庫:
GNU Multiple Precision Library (GMP) version 4.3.2 (or later)
Necessary to build GCC. If a GMP source distribution is found in a subdirectory of your GCC sources named gmp, it will be built together with GCC. Alternatively, if GMP is already installed but it is not in your library search path, you will have to configure with the --with-gmp configure option. See also --with-gmp-lib and --with-gmp-include.
MPFR Library version 2.4.2 (or later)
Necessary to build GCC. It can be downloaded from http://www.mpfr.org/. If an MPFR source distribution is found in a subdirectory of your GCC sources named mpfr, it will be built together with GCC. Alternatively, if MPFR is already installed but it is not in your default library search path, the --with-mpfr configure option should be used. See also --with-mpfr-lib and --with-mpfr-include.
MPC Library version 0.8.1 (or later)
Necessary to build GCC. It can be downloaded from http://www.multiprecision.org/. If an MPC source distribution is found in a subdirectory of your GCC sources named mpc, it will be built together with GCC. Alternatively, if MPC is already installed but it is not in your default library search path, the --with-mpc configure option should be used. See also --with-mpc-lib and --with-mpc-include.
在ftp://gcc.gnu.org/pub/gcc/infrastructure下載并按上述順序編譯。
設置環境變量LD_LIBRARY_PATH,包含上述3個庫下的lib目錄(重要!)否則會出現如cannot comput subfix(.o)等問題。
$ ../gcc-4.7.1/configure --prefix=/home/changweiwu/usr --with-gmp=/home/changweiwu/usr/ --with-mpfr=/home/changweiwu/usr --with-mpc=/home/changweiwu/usr/ --enable-languages=c,c++
以上在suse的機器上編譯通過,但在ubuntu下會有下述問題,需要進行設置頭文件、庫文件搜索路徑:
ISSUE#1
checking for suitable m4... configure: error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).
Solution:
$ sudo apt-get install m4
ISSUE#2
In file included from /usr/include/stdio.h:28:0,
from ../../../../gcc-4.7.0/libgcc/../gcc/tsystem.h:88,
from ../../../../gcc-4.7.0/libgcc/libgcc2.c:29:
/usr/include/features.h:324:26: fatal error: bits/predefs.h: No such file or directory
compilation terminated.
make[5]: *** [_muldi3.o] Error 1
Analysis:
Use 'locate bits/predefs.h' to find the path of this header. (in '/usr/include/x86_64-Linux-gnu')
Solution:
#export C_INCLUDE_PATH=/usr/include/i386-linux-gnu && export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH
ISSUE#3
In file included from /usr/include/features.h:389:0,
from /usr/include/stdio.h:28,
from ../../../../gcc-4.7.0/libgcc/../gcc/tsystem.h:88,
from ../../../../gcc-4.7.0/libgcc/libgcov.c:29:
/usr/include/i386-linux-gnu/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory
compilation terminated.
make[5]: *** [_gcov.o] Error 1
Analysis:
Related to libc multilib, disable it with '--disable-multilib' should work.
Solution:
add '--disable-multilib' and 'configure' again, then run 'make'.
ISSUE#4
/usr/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make[3]: *** [libgcc_s.so] Error 1
make[3]: *** Waiting for unfinished jobs....
Analysis:
Use 'locate crti.o' to find this file. (in '/usr/lib/i386-linux-gnu/crti.o') Set LIBRARY_PATH (LDFLAGS)
Solution:
#export LIBRARY_PATH=/usr/lib/i386-linux-gnu
make && make install