说明:
GCC4.7前段时间release了,这里记录一下编译GCC4.7的过程,以供查阅。
依赖:
编译GCC除了一些基本的编译工具如make、gcc等GNU工具之外,还有几个基本的依赖库:mpc,mpfr,gmp。如果没有安装这三个库,那么configure无法通过。可以通过apt-get的方式安装,但是更好的方式是自己编译,如果没有安装configure失败的时候就会提示你去哪里下载它们。下载后编译即可,然后对要编译的gcc在configure的时候通过--with-xxx告知。除了这三个库,还有ppl、cloog等库是可选的,没有也能成功编译gcc,当然,它们有它们特定的一些特性了,这里就不探讨了。
GCC下载:
(1)svn
通过svn下载,可以下载到很多分支,以及下载到最新的代码。
(2)mirrors:
http://gcc.gnu.org/mirrors.html
通过镜像下载,一般只有一些主要的releases。不会有分支的源码或者最新的源码。
下面的内容就是通过镜像下载的发布版的GCC4.7.0(gcc-4.7.0.tar.gz)的源码编译的过程。
(3)git
http://gcc.gnu.org/git/?p=gcc.git;a=summary (http://gcc.gnu.org/wiki/GitMirror)
其实也是mirrors,通过git的形式。这里的代码基本和svn都是对应的,所以也能下载到分支的代码和最新的代码。
总之,下载的方式很多,在gcc官网http://gcc.gnu.org/右边找找就知道了,之所以提到这些下载方式,是考虑到每个人环境不一样,网速不一样,可以根据需要选择。
说明:svn下载需要使用svn软件,mirrors和git可以直接通过浏览器下载,git也可以通过软件下载。
建议:编译较新的GCC的时候,尽量使用较新的64bit的系统去编译。这样一次成功的可能性更大。
类似文章推荐:
在 Ubuntu 12.04 下编译 GCC 4.7
Linux编译安装GCC 4.7
在ubuntu上编译gcc会到的问题及解决方法
下面是我在编译GCC4.7的时候的笔记,笔记绝对真实,从一个全新的系统开始每一步都记录了,所以只要你使用相同的环境,理论上应该可以成功。
How to build gcc in Ubuntu 12.04.txt
#################################### Notes
These are my notes about how to build gcc in ubuntu 12.04.
GCC version: 4.7.0 release
OS Enviroment:
64bit Ubuntu 12.04 LTS Beta 2, installed in vbox -> 'ubuntu-12.04-beta2-desktop-amd64.iso'
A newly installed clean OS, so it should work for you if you are using same OS.
What I did after install the OS?
Ok....though it is a newly install OS, i have done following before building gcc:
(these may not affect gcc build, but i like to do this before using a newly ubuntu....)
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install nautilus-open-terminal
sudo apt-get install binutils
sudo apt-get install vim
sudo apt-get install tree
Install bison:
(I did this as i know this is a known dependencies to build gcc, without installing bison/flex libraries, you will get some errors while building gcc....)
sudo apt-get install bison
####################################
1. download
download from GCC Mirror Sites (http://gcc.gnu.org/mirrors.html)
-> Use 'http://ftpmirror.gnu.org' to automatically choose a nearby and up-to-date mirror
Specificly, I downloaded from 'http://ftp.wayne.edu/pub/gnu/gcc/gcc-4.7.0/', package is: 'gcc-4.7.0.tar.gz'.
2. install gmp
#sudo mkdir -p /opt/gmp-4.3.2
#tar -jxvf gmp-4.3.2.tar.bz2
#cd gmp-4.3.2
#./configure --prefix=/opt/gmp-4.3.2
#make && make check && sudo make install
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3. install mpfr
#sudo mkdir -p /opt/mpfr-2.4.2
#tar -jxvf mpfr-2.4.2.tar.bz2
#cd mpfr-2.4.2
#./configure --prefix=/opt/mpfr-2.4.2 --with-gmp=/opt/gmp-4.3.2
#make && make check && sudo make install
4. install mpc
#sudo mkdir -p /opt/mpc-0.8.1
#tar -zxvf mpc-0.8.1.tar.gz
#cd mpc-0.8.1
#./configure --prefix=/opt/mpc-0.8.1 --with-gmp=/opt/gmp-4.3.2 --with-mpfr=/opt/mpfr-2.4.2
#make && make check && sudo make install
5. build gcc
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/gmp-4.3.2/lib:/opt/mpfr-2.4.2/lib:/opt/mpc-0.8.1/lib
#note: this setting is needed as while building gcc, it will generate some other binaries and they will need the .so libraries to run to do other tasks for building gcc...
#sudo mkdir -p /opt/gcc-4.7
#mkdir gcc_build
#../gcc-4.7.0/configure --prefix=/opt/gcc-4.7 --with-gmp=/opt/gmp-4.3.2 --with-mpfr=/opt/mpfr-2.4.2 --with-mpc=/opt/mpc-0.8.1
notes: --disable-multilib
#make -j8
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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/x86_64-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/x86_64-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/x86_64-linux-gnu/crti.o') Set LIBRARY_PATH (LDFLAGS)
Solution:
#export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ISSUE#5
libtool: compile: /home/sgeng2/Desktop/gcc_4_7/gcc_build/./gcc/xgcc -B/home/sgeng2/Desktop/gcc_4_7/gcc_build/./gcc/ -B/opt/gcc-4.7/x86_64-unknown-linux-gnu/bin/ -B/opt/gcc-4.7/x86_64-unknown-linux-gnu/lib/ -isystem /opt/gcc-4.7/x86_64-unknown-linux-gnu/include -isystem /opt/gcc-4.7/x86_64-unknown-linux-gnu/sys-include /home/sgeng2/Desktop/gcc_4_7/gcc-4.7.0/libobjc/accessors.m -c -I. -I/home/sgeng2/Desktop/gcc_4_7/gcc-4.7.0/libobjc -g -O2 -W -Wall -Wwrite-strings -Wstrict-prototypes -DIN_GCC -DIN_TARGET_LIBS -fno-strict-aliasing -fexceptions -I/home/sgeng2/Desktop/gcc_4_7/gcc-4.7.0/libobjc/../gcc -I/home/sgeng2/Desktop/gcc_4_7/gcc-4.7.0/libobjc/../gcc/config -I../.././gcc -I/home/sgeng2/Desktop/gcc_4_7/gcc-4.7.0/libobjc/../libgcc -I../libgcc -I/home/sgeng2/Desktop/gcc_4_7/gcc-4.7.0/libobjc/../include -fgnu-runtime -fPIC -DPIC -o .libs/accessors.o
In file included from /usr/include/string.h:27:0,
from /home/sgeng2/Desktop/gcc_4_7/gcc-4.7.0/libobjc/accessors.m:28:
/usr/include/features.h:324:26: fatal error: bits/predefs.h: No such file or directory
compilation terminated.
make[2]: *** [accessors.lo] Error 1
Analysis:
We have set C_INCLUDE_PATH and CPLUS_INCLUDE_PATH, but still get this error? check carefully and it is building an obj-c file, i am building gcc for all language support (including c/c++/fotran/objc/go/java...), so, the only reason should be: the search path var is different for obj-c, by guess, i tried OBJC_INCLUDE_PATH and it works (as i do not have any experience on obj-c, i have to guess)!
Solution:
#export OBJC_INCLUDE_PATH=$C_INCLUDE_PATH
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#make check -> optional
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ISSUE#6
#make check
make[1]: Entering directory `/home/sgeng2/Desktop/gcc_4_7/gcc_build'
make[2]: Entering directory `/home/sgeng2/Desktop/gcc_4_7/gcc_build/fixincludes'
autogen -T ../../gcc-4.7.0/fixincludes/check.tpl ../../gcc-4.7.0/fixincludes/inclhack.def
/bin/bash: autogen: command not found
make[2]: *** [check] Error 127
make[2]: Leaving directory `/home/sgeng2/Desktop/gcc_4_7/gcc_build/fixincludes'
make[1]: *** [check-fixincludes] Error 2
make[1]: Leaving directory `/home/sgeng2/Desktop/gcc_4_7/gcc_build'
make: *** [do-check] Error 2
Solution:
#sudo apt-get install autogen
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#sudo make install
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ISSUE#7
libtool: relink: /home/sgeng2/Desktop/gcc_4_7/gcc_build/./gcc/xgcc -B/home/sgeng2/Desktop/gcc_4_7/gcc_build/./gcc/ -B/opt/gcc-4.7/x86_64-unknown-linux-gnu/bin/ -B/opt/gcc-4.7/x86_64-unknown-linux-gnu/lib/ -isystem /opt/gcc-4.7/x86_64-unknown-linux-gnu/include -isystem /opt/gcc-4.7/x86_64-unknown-linux-gnu/sys-include -shared .libs/backtrace.o .libs/bounds.o .libs/compile_options.o .libs/convert_char.o .libs/environ.o .libs/error.o .libs/fpu.o .libs/main.o .libs/memory.o ..........................................................
/usr/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
libtool: install: error: relink `libgfortran.la' with the above command before installing it
make[4]: *** [install-toolexeclibLTLIBRARIES] Error 1
make[4]: Leaving directory `/home/sgeng2/Desktop/gcc_4_7/gcc_build/x86_64-unknown-linux-gnu/libgfortran'
make[3]: *** [install-am] Error 2
Analysis:
/usr/bin/ld: cannot find crti.o: No such file or directory
Why?? I have already set LIBRARY_PATH!! Is it possible that here it is invoke '/home/sgeng2/Desktop/gcc_4_7/gcc_build/./gcc/xgcc' and this xgcc will not read LIBRARY_PATH? I tried to set GCC_EXEC_PREFIX and also failed to work. Whatever, 'ld' should search '/usr/lib64', so another workaround is to make a link in /usr/lib64 to /usr/lib/x86_64-linux-gnu
Solution:
#sudo ln -s /usr/lib/x86_64-linux-gnu /usr/lib64
Note: not only to make link fo crti.o as there are some other crt* files to be used by 'ld', you can also make link one by one.
Note: 'crti.o' is in package 'libgc6-dev', if it is not found in your OS, try to install this package. (for ubuntu, apt-get install)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ISSUE#8
test -z "/opt/gcc-4.7/libexec/gcc/x86_64-unknown-linux-gnu/4.7.0" || /bin/mkdir -p "/opt/gcc-4.7/libexec/gcc/x86_64-unknown-linux-gnu/4.7.0"
Installing dummy lib libgcj_bc.so.1.0.0
/home/sgeng2/Desktop/gcc_4_7/gcc_build/./gcc/cc1: error while loading shared libraries: libmpfr.so.1: cannot open shared object file: No such file or directory
make[5]: *** [install-exec-hook] Error 1
Analysis:
This is quite an similiar issue with ISSUE#7, both strange!!! I have set LD_LIBRARY_PATH, and also, i tried with "ldd /home/sgeng2/Desktop/gcc_4_7/gcc_build/./gcc/cc1' and it can work, but it will faile while 'make install'! It is crazy!
Solution:
#sudo ln -s /opt/mpfr-2.4.2/lib/libmpfr.so.1 /usr/lib64/libmpfr.so.1
#sudo ln -s /opt/gmp-4.3.2/lib/libgmp.so.3 /usr/lib64/libgmp.so.3
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6. Other useful options you may use to build gcc
--enable-languages=c,c++,fortran,go,java...
--program-suffix=-4.7
--with-system-zlib
--enable-threads=posix (note: seems this should be default.)
Suggestion: add '--enable-languages=c,c++' if you only need c/c++ compiler, without this setting, the compiling time may be too long.
7. Create a 'gccvars.sh' file to help setup gcc environment...
optional but strongly suggested....
# filename: gccvars.sh
# 'source gccvars.sh' to set the environment of gcc
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# filename: gccvars.sh
# 'source gccvars.sh' to set the environment of gcc
export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH
export OBJC_INCLUDE_PATH=$C_INCLUDE_PATH
export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LIBRARY_PATH
export GCCDIR=/opt/gcc-4.7
export PATH=$GCCDIR/bin:$PATH
export LD_LIBRARY_PATH=$GCCDIR/lib:$GCCDIR/lib64:/opt/gmp-4.3.2/lib:/opt/mpfr-2.4.2/lib:/opt/mpc-0.8.1/lib:$LD_LIBRARY_PATH
export MANPATH=$GCCDIR/share/man:$MANPATH
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7. Test the built gcc
It seems no 'go compiler' generated? I can see 'c/c++/fortran/java' compiler. Whatever, ignore this as i am not willing to use go compiler.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// filename: test.cpp
// Program to test the new C++11 lambda syntax
// g++ test.cpp -std=c++11 -o a.out
#include <iostream>
using namespace std;
int main() {
cout << [](int m, int n) { return m + n;} (2,4) << endl;
return 0;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
! filename: test.f90
! Program to test 'helloworld' of fotran compiler
! gfortran test.f90 -o a.out
program main
print *,'hello world'
end
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++