2013-08-12 wcdj (明天七夕,纪念结婚一周年)
1 下载最新支持C++11的GCC编译器
http://gcc./
http://gcc./gcc-4.8/(下载gcc 4.8.1)
- GCC 4.8.1 will beC++11 feature-complete[2013-04-01]
- Support for C++11 ref-qualifiers was added to the GCC 4.8 branch, making G++ the first C++ compiler to implement all the major language features of the C++11 standard. This functionality will be available in GCC 4.8.1. (注意并不是所有的新特性都支持)
2 编译遇到的问题
首先查看INSTALL说明:file:///Users/gerryyang/code/public/gcc-4.8.1/INSTALL/index.html,然后尝试编译:
sh-3.2#pwd
/Users/gerryyang/code/public/gcc-4.8.1
- sh-3.2# ./configure --prefix=/usr/local/gcc-4.8.1/
- checking build system type... x86_64-apple-darwin12.4.0
- checking host system type... x86_64-apple-darwin12.4.0
- checking target system type... x86_64-apple-darwin12.4.0
- checking for a BSD-compatible install... /usr/bin/install -c
- checking whether ln works... yes
- checking whether ln -s works... yes
- checking for a sed that does not truncate output... /usr/bin/sed
- checking for gawk... no
- checking for mawk... no
- checking for nawk... no
- checking for awk... awk
- checking for libatomic support... yes
- checking for libitm support... yes
- checking for libsanitizer support... yes
- checking for gcc... gcc
- checking for C compiler default output file name... a.out
- checking whether the C compiler works... yes
- checking whether we are cross compiling... no
- checking for suffix of executables...
- checking for suffix of object files... o
- checking whether we are using the GNU C compiler... yes
- checking whether gcc accepts -g... yes
- checking for gcc option to accept ISO C89... none needed
- checking for g++... g++
- checking whether we are using the GNU C++ compiler... yes
- checking whether g++ accepts -g... yes
- checking whether g++ accepts -static-libstdc++ -static-libgcc... no
- checking for gnatbind... no
- checking for gnatmake... no
- checking whether compiler driver understands Ada... no
- checking how to compare bootstrapped objects... cmp --ignore-initial=16 $$f1 $$f2
- checking for objdir... .libs
- checking for the correct version of gmp.h... no
- configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
- Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
- their locations. Source code for these libraries can be found at
- their respective hosting sites as well as at
- ftp://gcc./pub/gcc/infrastructure/. See also
- http://gcc./install/prerequisites.html for additional info. If
- you obtained GMP, MPFR and/or MPC from a vendor distribution package,
- make sure that you have installed both the libraries and the header
- files. They may be located in separate packages.
OK,根据错误提示“configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.”下载依赖的packages,ftp路径也已提示我们“ftp://gcc./pub/gcc/infrastructure/",可以分别从如下链接进行下载:
GMP 4.2+:
ftp://gcc./pub/gcc/infrastructure/gmp-4.3.2.tar.bz2
MPFR 2.4.0+:
ftp://gcc./pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2
MPC 0.8.0+:
ftp://gcc./pub/gcc/infrastructure/mpc-0.8.1.tar.gz
根据提示的顺序分别安装GMP,MPFR和MPC:
step1: ./configure --prefix=/usr/local/gmp-4.3.2; make install
step2: ./configure --prefix=/usr/local/mpfr-2.4.2 --with-gmp=/usr/local/gmp-4.3.2/; make install
注意:step2依赖于step1,否则会报”configure: error: gmp.h can't be found, or is unusable.“的错误。
step3: ./configure --prefix=/usr/local/mpc-0.8.1 --with-gmp=/usr/local/gmp-4.3.2/ --with-mpfr=/usr/local/mpfr-2.4.2/; make install
注意:step3同样依赖于前面的库,否则会报”configure: error: libgmp not found or uses a different ABI.和configure: error: libmpfr not found or uses a different ABI.“的错误。
step4: 设置环境变量,export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gmp-4.3.2/lib/:/usr/local/mpfr-2.4.2/lib/:/usr/local/mpc-0.8.1/lib/
step5:./configure --prefix=/usr/local/gcc-4.8.1/ --enable-threads=posix --disable-checking --with-gmp=/usr/local/gmp-4.3.2/ --with-mpfr=/usr/local/mpfr-2.4.2/ --with-mpc=/usr/local/mpc-0.8.1/ (还是提示找不到!看来掉坑里了)
3 正确地编译方法
上述方法在参考[2]是不建议这样做的:
The difficult way, which is not recommended, is to download the sources for GMP, MPFR and MPC, then configure and install each of them in non-standard locations, then configure GCC with--with-gmp=/some/silly/path/gmp --with-mpfr=/some/silly/path/mpfr --with-mpc=/some/silly/path/mpc,
then be forced to set LD_LIBRARY_PATH=/some/silly/path/gmp:/some/silly/path/mpfr:/some/silly/path/mpc/lib in your environment forever. This is silly and causes major problems for anyone who doesn't understand how dynamic linkers find
libraries at runtime. Do not do this. If building GCC fails when using any of the--with-gmp or--with-mpfr or--with-mpc options then you probably shouldn't be using them.
参考[2]中介绍了更简单的方法:
Alternatively, after extracting the GCC source archive, simply run the
./contrib/download_prerequisites script in the GCC source directory. That will download the support libraries and create symlinks, causing them to be built automatically as part of the GCC
build process. Set GRAPHITE_LOOP_OPT=yes in the script if you want to build GCC with the Graphite loop optimizations.
- # gcc-4.8.1/contrib/download_prerequisites
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see http://www./licenses/.
-
- MPFR=mpfr-2.4.2
- GMP=gmp-4.3.2
- MPC=mpc-0.8.1
-
- wget ftp://gcc./pub/gcc/infrastructure/$MPFR.tar.bz2 || exit 1
- tar xjf $MPFR.tar.bz2 || exit 1
- ln -sf $MPFR mpfr || exit 1
-
- wget ftp://gcc./pub/gcc/infrastructure/$GMP.tar.bz2 || exit 1
- tar xjf $GMP.tar.bz2 || exit 1
- ln -sf $GMP gmp || exit 1
-
- wget ftp://gcc./pub/gcc/infrastructure/$MPC.tar.gz || exit 1
- tar xzf $MPC.tar.gz || exit 1
- ln -sf $MPC mpc || exit 1
-
- rm $MPFR.tar.bz2 $GMP.tar.bz2 $MPC.tar.gz || exit 1
前提需要下载wget(使用源码方式,这里有些"坑",若不指定--with-ssl=openssl则提示需要安装GnuTLS)
- ...
- checking for compress in -lz... yes
- checking for gpg_err_init in -lgpg-error... no
- checking for gcry_control in -lgcrypt... no
- checking for libgnutls... no
- configure: error: --with-ssl was given, but GNUTLS is not available.
Note the with-ssl=openssl option in the command above. If you omit that, you will get the following error:
下载wget源代码地址:
http://www./software/wget/
tar -xzvf wget-1.14.tar.gz
cd wget-1.14
./configure --with-ssl=openssl
make
sudo make install
安装好wget后,使用代码中自带的脚本下载gcc依赖的文件:
- sh-3.2# pwd
- /Users/gerryyang/code/public/gcc-4.8.1
- sh-3.2# ./contrib/download_prerequisites
- --2013-08-12 22:37:39-- ftp://gcc./pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2
- => ‘mpfr-2.4.2.tar.bz2’
- Resolving gcc.... 209.132.180.131
- Connecting to gcc.|209.132.180.131|:21... connected.
- Logging in as anonymous ... Logged in!
- ==> SYST ... done. ==> PWD ... done.
- ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
- ==> SIZE mpfr-2.4.2.tar.bz2 ... 1077886
- ==> PASV ... done. ==> RETR mpfr-2.4.2.tar.bz2 ... done.
- Length: 1077886 (1.0M) (unauthoritative)
-
- 100%[==================================================================================================================================================================>] 1,077,886 194KB/s in 5.7s
-
- 2013-08-12 22:37:51 (185 KB/s) - ‘mpfr-2.4.2.tar.bz2’ saved [1077886]
-
- --2013-08-12 22:37:51-- ftp://gcc./pub/gcc/infrastructure/gmp-4.3.2.tar.bz2
- => ‘gmp-4.3.2.tar.bz2’
- Resolving gcc.... 209.132.180.131
- Connecting to gcc.|209.132.180.131|:21... connected.
- Logging in as anonymous ... Logged in!
- ==> SYST ... done. ==> PWD ... done.
- ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
- ==> SIZE gmp-4.3.2.tar.bz2 ... 1897483
- ==> PASV ... done. ==> RETR gmp-4.3.2.tar.bz2 ... done.
- Length: 1897483 (1.8M) (unauthoritative)
-
- 100%[==================================================================================================================================================================>] 1,897,483 177KB/s in 10s
-
- 2013-08-12 22:38:08 (177 KB/s) - ‘gmp-4.3.2.tar.bz2’ saved [1897483]
-
- --2013-08-12 22:38:09-- ftp://gcc./pub/gcc/infrastructure/mpc-0.8.1.tar.gz
- => ‘mpc-0.8.1.tar.gz’
- Resolving gcc.... 209.132.180.131
- Connecting to gcc.|209.132.180.131|:21... connected.
- Logging in as anonymous ... Logged in!
- ==> SYST ... done. ==> PWD ... done.
- ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done.
- ==> SIZE mpc-0.8.1.tar.gz ... 544950
- ==> PASV ... done. ==> RETR mpc-0.8.1.tar.gz ... done.
- Length: 544950 (532K) (unauthoritative)
-
- 100%[==================================================================================================================================================================>] 544,950 172KB/s in 3.1s
-
- 2013-08-12 22:38:16 (172 KB/s) - ‘mpc-0.8.1.tar.gz’ saved [544950]
再尝试编译gcc,configure成功生产Makefile,下面可以make install了。
注意:刚开始没注意,在源文件目录(gcc-4.8.1/)下configure后再进行make; make install会出现编译时的断言错误,在网上搜了错误原因暂没找到具体的解决办法。通过查看参考[2]通过另一种方法可以避免上述那个断言错误:
See
Installing GCC: Configuration for the full documentation. A major benefit of running srcdir/configure from outside the source directory (instead of running./configure) is that the source directory
will not be modified in any way, so if your build fails or you want to re-configure and build again, you simply delete everything in the objdir and start again.
For example, configuring and building GCC 4.6.2 should be as simple as:
tar xzf gcc-4.6.2.tar.gz
cd gcc-4.6.2
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
$PWD/../gcc-4.6.2/configure --prefix=$HOME/gcc-4.6.2
make
make install
If your build fails and your configure command has lots of complicated options you should try removing options and keep it simple. Do not add lots of configure options you don't
understand, they might be the reason your build fails.
OK,通过上述方法进行编译问题解决,编译耗时1小时+,下面是编译成功后gcc的版本信息:

4 参考
[1]
http:///questions/9450394/how-to-install-gcc-from-scratch-with-gmp-mpfr-mpc-elf-without-shared-librari (讲的很清楚)
[2]
http://gcc./wiki/InstallingGCC(找到了光明大道)
http://gcc./install/prerequisites.html(关于一些依赖的说明)
[3]
http://crosstown./os-x/32-install-and-configure-wget-on-os-x-lion-107 (编译wget遇到的坑,这里说明了一种解决方法)
[4]
http:///blog/install-wget-on-os-x-lion/ (编译wget遇到的坑)
|