編譯glibc(gcc)以及過程中遇到的一些錯誤
一定要按照configure ——> make ——> make install三步驟。
1、下載glibc,解壓
原文中的glibc-ports是用來支援arm架構晶片的包,可以將包內檔案移動到glibc-2.15/ports內編譯安裝,這裡不再贅述
wget http://ftp.gnu.org/gnu/glibc/glibc-2.15.tar.gz
tar -zxvf glibc-2.15.tar.gz
2、準備一個編譯資料夾
mkdir glibc-2.15-build
cd glibc-2.15-build
3、配置configure
../glibc-2.15/configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
4、編譯安裝
make
make install
順便說一下,make的-j引數是個坑,雖然可以並行編譯,但如果Makefile或依賴關係有問題,編譯會出錯。
5、檢查軟連線
編譯成功後,glibc-2.15-build檔案下會生成一個新的libc.so.6,這是一個軟連線,而真實的lib檔案是此目錄下的libc.so。ll是ls -l的別名
ll libc.so.6
檢視庫檔案
ll /lib64/libc*
可以看到2.12的舊庫檔案還在,多了2.15版本的庫檔案,而且軟連結檔案全部指向了2.15版本
檢視glibc支援的版本
strings libc.so | grep GLIBC
下面總結了一下編譯glibc遇到的錯誤。
DLIBRARYPATH shouldn’t contain the current directory
checking LD_LIBRARY_PATH variable... contains current directory configure: error: *** LD_LIBRARY_PATH shouldn't contain the current directory when *** building glibc. Please change the environment variable *** and run
出現這個錯誤的原因是由於環境變數的LDLIBRARYPATH中出現了當前目錄,這對gcc編譯來說是多餘的。
解決方法:
export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/
或者
unset LD_LIBRARY_PATH
安裝目錄prefix沒指定的錯誤:
*** On GNU/Linux systems the GNU C Library should not be installed into *** /usr/local since this might make your system totally unusable. *** We strongly advise to use a different prefix. For details read the FAQ. *** If you really mean to do this, run configure again using the extra *** parameter `--disable-sanity-checks'.
出現這個錯誤的原因在於,如果沒指定prefix,則系統把這個程式裝在/usr/local目錄下。但是,c標準庫要求必須安裝在與根檔案系統相同的分割槽中,否則不能用。但是/usr這個掛載點可能處在外部的分割槽。
解決方法就是指定prefix到/usr,這時候configure程式會自動把必須的內容裝在/lib下(這個目錄肯定與根目錄同分區),擴充套件內容再裝到/usr/lib下。
有些安裝方法是編譯時候指定的目錄不是/usr,而是通過建立軟鏈指向新的libc-2.14.so版本,在此過程中需要刪除原來連線,建立新的軟連線,但是此處有一個大坑,就是當你刪除libc.so.6之後會導致系統命令不可用,如下在測試機中演示的錯誤過程:
rm -rf /lib64/libc.so.6
接下來當你建立新的軟連結時候,會發現ln命令不能用了。
ln -s /lib64/libc-2.14.so /lib64/libc.so.6
ln: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
當出現上面的狀況時候,可以使用以下方法解決(假設libc-2.14.so已經拷貝到/lib64/目錄下):
LD_PRELOAD=/lib64/libc-2.14.so ln -s /lib64/libc-2.14.so /lib64/libc.so.6
當然如果升級失敗,還可以使用下面命令還原至系統升級前的版本libc-2.12.so:
LD_PRELOAD=/lib64/libc-2.12.so ln -s /lib64/libc-2.12.so /lib64/libc.so.6
“LD_PRELOAD”是一個環境變數,定義在程式執行前優先載入的動態連結庫,本處作用就是在執行後面的ln命令時,指定使用的glibc庫,這樣命令就可以正常使用了。