【筆記】CentOS上源碼安裝GCC 4.8.2

CentOS上源碼安裝GCC 4.8.2

gcc --version  # 查看gcc版本
sudo yum update gcc -y  # 只能升到4.4.7

1) 前提

參考Prerequisites for GCC,須要GMP, MPFR, MPC, ISL, CLooG。php

我先查看了下系統,發現原生就裝了GMP等,以下命令。但以後"gcc ./configure"有錯,仍須要下載安裝GMP等。詳見"Issue 1"。html

yum list installed gmp  # 查看gmp安裝了沒

ps: 上述問題,應該是要"yum install gmp-devel"來安裝開發環境就能夠了。本來只有運行環境,因此是"Issue 1"裏提到的找不到頭文件。MPFR,MPC等也一樣。linux

2) 準備

缺乏的均可以先到ftp://gcc.gnu.org/pub/gcc/infrastructure/下找找看。ios

# 建立個目錄,都下載到這裏
mkdir -p ~/Env/gcc; cd ~/Env/gcc

# GMP, MPFR, MPC
wget https://ftp.gnu.org/gnu/gmp/gmp-5.1.3.tar.bz2
wget http://www.mpfr.org/mpfr-current/mpfr-3.1.2.tar.gz
wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.2.tar.gz
tar jxvf gmp-5.1.3.tar.bz2
tar zxvf mpfr-3.1.2.tar.gz
tar zxvf mpc-1.0.2.tar.gz
# 都直接安裝到默認路徑下
cd gmp-5.1.3
./configure --enable-cxx CPPFLAGS=-fexceptions  # 選項緣由"Issue 2"
make; make install
cd ../mpfr-3.1.2; ./configure
make; make install
cd ../mpc-1.0.2; ./configure
make; make install
cd ..

若是GMP、MPFR、MPC在"./configure"時用"--prefix"指定了安裝路徑,則"gcc ./configure"須要加上"--with-xxx"等選項。由GMP > MPFR > MPC依賴順序,也須要加上"--with-xxx"選項。可見參考34。c++

以後"gcc ./configure"(見"3) 安裝"),我這兒仍有錯,ISL與CLooG也要安裝:測試

sudo gedit config.log  # 查看日誌,搜索"error"

# issue: configure: error: C++ compiler missing or inoperational
# 沒有C++編譯器
yum install gcc-c++

# issue: conftest.cpp:11:2: error: #error -static-libstdc++ not implemented
# 沒有C,C++靜態庫
yum install glibc-static libstdc++-static -y
# 但報"No package libstdc++-static available",即沒有-static-libstdc++源,問題仍存在。
# "checking whether g++ accepts -static-libstdc++ -static-libgcc"時報出的,可忽略。

# issue: conftest.c:10:25: error: isl/version.h: No such file or directory
# 沒有ISL
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.12.2.tar.bz2
tar jxvf isl-0.12.2.tar.bz2
cd isl-0.12.2; ./configure
make; make install
cd ..

# issue: ./conftest: error while loading shared libraries: libisl.so.10: cannot open shared object file: No such file or directory
# 在"/usr/local/lib"目錄下,怎麼就它找不到。加到"/etc/ld.so.conf"或用"LD_LIBRARY_PATH"。
vi /etc/ld.so.conf  # 添加"/usr/local/lib"
ldconfig  # 重建/etc/ld.so.cache
# 自問:於**Issue 1**裏,已經單獨在"/etc/ld.so.conf.d"下建個"*.conf"添加過"/usr/local/lib",怎麼沒效果呢?
# 自答:以後安裝的ISL,沒從新ldconfig,因此找不到了?當時沒查,不能確認。用如下命令:
strings /etc/ld.so.cache | grep libisl  # 查看
# 以後,刪除了"/etc/ld.so.conf"內的添加,也再也不有該問題。

# issue: conftest.c:10:27: error: cloog/version.h: No such file or directory
# 沒有CLooG
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-0.18.1.tar.gz
tar zxvf cloog-0.18.1.tar.gz
cd cloog-0.18.1; ./configure
make; make install
cd ..

# issue: conftest.c:15: error: 'choke' undeclared (first use in this function)
# "checking for version 0.17.0 of CLooG"失敗報出的,沒問題。

若是安裝時都自定義了路徑,記得要將庫路徑都添加到"/etc/ld.so.conf"內。ui

3) 安裝

# GCC
wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.gz  # 下載gcc源碼
tar zxvf gcc-4.8.2.tar.gz  # 解壓源碼

cd gcc-4.8.2  # 進入源碼目錄
# 生成Makefile,./configure --help看幫助,或看參考2
./configure \
--prefix=/usr/local/gcc-4.8.2 \
--enable-languages=c,c++,go \
--disable-multilib

make; make install  # 編譯並安裝

# 重建gcc軟連接
cd /usr/bin
mv gcc gcc4.4.7
mv g++ g++4.4.7
ln -s /usr/local/gcc-4.8.2/bin/gcc gcc
ln -s /usr/local/gcc-4.8.2/bin/g++ g++

# 添加gcc的man路徑
vi /etc/man.config
# 添加"MANPATH /usr/local/gcc-4.8.2/share/man"
# 因爲未卸載舊的,我這"man gcc"能夠查看幫助,因此未添加。
# 若是再添加,不肯定會有什麼影響。

4) 問題

Issue 1:this

king 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..net

開始沒注意"king for the correct version of gmp.h... no",先以下查了gmp等的位置。c++11

rpm -qa|grep gmp  # 查看gmp安裝了哪些
rpm -ql gmp-4.3.1-7.el6_2.2.i686  # 查看gmp安裝位置

而後添加"--with-gmp"等,沒效果。

難道庫搜索路徑沒包括"/usr/lib",卻包括了"/usr/local/lib"(以後安裝了GMP等後,卻能經過"./configure")?

因而,查看"/etc/ld.so.conf",其"include ld.so.conf.d/*.conf",但繼續查看發現都不包含上述兩路徑,不是很明白。而後,自行添加了下。

cd /etc/ld.so.conf.d
touch mine.conf  # 或用vi,發現我不會用了
gedit mine.conf  # 添加/usr/lib與/usr/local/lib
ldconfig  # 重建/etc/ld.so.cache

注意,安裝ISL後,執行ldconfig會有以下錯誤,重裝換個低版本也同樣。試着解決沒成功,而後忽視了。相似提問:Pacman/ldconfig - wrong magic bytes

ldconfig: /usr/local/lib/libisl.so.10.2.2-gdb.py is not an ELF file - it has the wrong magic bytes at the start.

vi命令參考[1][2]

以後,發現GMP等頭文件沒有:

find / | grep gmp.h

因此仍是要安裝的。

Issue 2:

# 沒有PPL
wget ftp://ftp.cs.unipr.it/pub/ppl/releases/1.1/ppl-1.1.tar.bz2
tar jxvf ppl-1.1.tar.bz2
cd ppl-1.1; ./configure
make; make install
cd ..

"ppl ./configure"時,

若是"gmp ./configure"未設置"--enable-cxx":

checking for the GMP library version 4.1.3 or above... no configure: error: Cannot find GMP version 4.1.3 or higher. GMP is the GNU Multi-Precision library: see http://www.swox.com/gmp/ for more information. When compiling the GMP library, do not forget to enable the C++ interface: add --enable-cxx to the configuration options.

若是"gmp ./configure"未設置"CPPFLAGS=-fexceptions":

configure: WARNING: CANNOT PROPAGATE EXCEPTIONS BACK FROM GMP: *** MEMORY EXHAUSTION MAY RESULT IN ABRUPT TERMINATION. *** This is OK, if you do not plan to use the bounded memory capabilities *** offered by the PPL. Otherwise, if you are using GCC or the Intel C/C++ *** compiler, please make sure you use a version of GMP compiled with the *** `-fexceptions' compiler option. *** To build such a version, you can configure GMP as follows: *** CPPFLAGS=-fexceptions ./configure --enable-cxx --prefix=/usr/local

按要求重編了GMP,但以上這個警號還會在。

"ppl make"時,

mp_std_bits.defs.hh:47: error: redefinition of 'class std::numeric_limits<__gmp_expr<__mpz_struct [1], __mpz_struct [1]> >' /usr/local/include/gmpxx.h:3271: error: previous definition of 'class std::numeric_limits<__gmp_expr<__mpz_struct [1], __mpz_struct [1]> >' mp_std_bits.defs.hh:108: error: redefinition of 'class std::numeric_limits<__gmp_expr<__mpq_struct [1], __mpq_struct [1]> >' /usr/local/include/gmpxx.h:3308: error: previous definition of 'class std::numeric_limits<__gmp_expr<__mpq_struct [1], __mpq_struct [1]> >'

ppl-0.11與gmp-5.1.3有衝突,改成ppl-1.1便可。

ppl-0.11: ftp://gcc.gnu.org/pub/gcc/infrastructure/ppl-0.11.tar.gz

5) 測試

test.cc:

#include <iostream>
#include <string>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    for (auto& el : vec)
    {
        os << el << ' ';
    }
    return os;
}

int main()
{
    std::vector<std::string> words = {
        "Hello", "from", "GCC", __VERSION__, "!"
    };
    std::cout << words << std::endl;
}

而後:

g++ -std=c++11 test.cc -o test
./test

輸出:"Hello from GCC 4.8.2 !"。

6) 參考

  1. Prerequisites for GCC
  2. Installing GCC: Configuration
  3. CentOS6.4下源碼安裝gcc-4.8.1
  4. CentOS6.2下編譯gcc4.8.2
  5. GCC 4.8.2 編譯安裝小記

p.s. 升級GCC後,動態庫連接問題

相關文章
相關標籤/搜索