原文 http://blog.atime.me/note/install-gcc-5.2.0-from-source.html
記錄編譯GCC 5.2.0時遇到的問題和解決方法,以備日后查詢。
平時使用的服務器是CentOS5,自帶的gcc編譯器還是8年前發布的4.1.2版本,完全沒法寫C++11的代碼,因為不想升級操作系統,只好自己下載源碼編譯。
安裝過程挺dan疼的,只好記錄下來。
安裝依賴庫
GCC依賴於gmp 4.2+, mpfr 2.4+和mpc 0.8+,這里直接下載安裝最新的版本。
為了省事,所有的庫都直接裝到/usr/local目錄下的對應目錄。
安裝gmp 6.0
wget https://gmplib.org/download/gmp/gmp-6.0.0a.tar.bz2 tar xvf gmp-6.0.0a.tar.bz2 cd gmp-6.0.0 ./configure make -j4 make install
安裝mpfr 3.1.3
mpfr依賴於gmp。
wget http://www.mpfr.org/mpfr-current/mpfr-3.1.3.tar.bz2
tar xvf mpfr-3.1.3.tar.bz2 cd mpfr-3.1.3 ./configure --with-gmp-include=/usr/local/include \ --with-gmp-lib=/usr/local/lib make -j4 make install
安裝mpc 1.0.3
mpc依賴於gmp和mpfr。
wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
tar xvf mpc-1.0.3.tar.gz cd mpc-1.0.3 ./configure --with-mpfr-include=/usr/local/include \ --with-mpfr-lib=/usr/local/lib \ --with-gmp-include=/usr/local/include \ --with-gmp-lib=/usr/local/lib make -j4 make install
安裝GCC
編譯
建議先閱讀下官方的 安裝文檔 。
下載GCC並解壓。
wget ftp://ftp.gnu.org/gnu/gcc/gcc-5.2.0/gcc-5.2.0.tar.bz2 tar xvf gcc-5.2.0.tar.bz2 cd gcc-5.2.0
先unset若干個系統變量,以免出現某些宏找不到的情況。
unset CPLUS_INCLUDE_PATH LIBRARY_PATH
配置GCC
./configure \
--with-gmp-include=/usr/local/include \ --with-gmp-lib=/usr/local/lib \ --with-mpfr-include=/usr/local/include \ --with-mpfr-lib=/usr/local/lib \ --with-mpc-include=/usr/local/include \ --with-mpc-lib=/usr/local/lib \ --enable-languages=c,c++ \ --enable-threads=posix \ --disable-multilib
詳細的配置項說明可參考 安裝文檔 ,這里只編譯c和c++的編譯器。
然后 make -j8
,啟用多線程編譯。
測試
先安裝dejagnu: yum install dejagnu
。
然后運行如下命令:
make -j8 check-gcc
查看測試結果:
./contrib/test_summary
安裝
如果編譯順利通過, make install
即可。
gcc和g++默認被安裝到 /usr/local/bin
目錄下,libgcc和libstdc++默認被安裝到/usr/local/lib64
(x64)。
記得更下下動態庫緩存。
ldconfig
可能遇到的問題
XXXX not defined
遇到某個宏沒有定義的情況,先unset C_INCLUDE_PATH
再嘗試。
braced spec is invalid
很dan疼的一個問題,搜遍了全網也沒見有比較正式的解決方案。目前看上去比較靠譜的方法可參考 這里 ,具體操作就是手動改一下某個specs文件。
我這里是 host-x86_64-unknown-linux-gnu/gcc/specs
,把其中所有的%:sanitize(xxx)
改為 fsanitize=xxx
。
測試C++11
寫一個腦殘的cpp測試下新安裝的編譯器。
#include <atomic> #include <iostream> using namespace std; int main() { atomic<long long> num(1L << 14); cout << ++num << endl; }
編譯並運行:
/usr/local/bin/g++ -std=c++11 b.cpp -o b
LD_LIBRARY_PATH=/usr/local/lib64 ./b
--------------------------------------------------------------------------
編譯中的問題:
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"失敗報出的,沒問題。