编译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
网友评论