# Install GCC, G++ From Source Code Without Sudo ## 工作站環境 ```bash= [USERNAME@XXXXX ~]$ hostnamectl Static hostname: XXXXXXXXXXX Icon name: computer-desktop Chassis: desktop Machine ID: XXXXXXXXXXX Boot ID: XXXXXXXXXXX Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-229.11.1.el7.x86_64 Architecture: x86-64 ``` ```bash= [USERNAME@XXXXX ~]$ cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) ``` ```bash= [USERNAME@XXXXX ~]$ echo $0 -tcsh ``` ## 編譯 & 安裝 ```bash= # 下載要安裝的版本 (gcc-13.2.0) wget https://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.xz --no-check-certificate # 解壓縮 && 進入目錄 tar xf gcc-13.2.0.tar.xz && cd gcc-13.2.0 # 配置依賴項 ./contrib/download_prerequisites # 安裝 ./configure --disable-multilib --enable-languages=c,c++ --enable-threads=posix make -j make install DESTDIR=$HOME/.local/gcc-13.2.0 ``` ## 在 ~/.tcshrc 中加入下面幾行 ```bash= # for custom gcc, g++ setenv PATH $HOME/.local/gcc-13.2.0/usr/local/bin/:$PATH if ( ! $?LD_LIBRARY_PATH ) then setenv LD_LIBRARY_PATH $HOME/.local/gcc-13.2.0/usr/local/lib/:$HOME/.local/gcc-13.2.0/usr/local/lib64/ else setenv LD_LIBRARY_PATH $HOME/.local/gcc-13.2.0/usr/local/lib/:$HOME/.local/gcc-13.2.0/usr/local/lib64/:$LD_LIBRARY_PATH endif # end for custom gcc, g++ ``` - 如果沒設定 LD_LIBRARY_PATH 也能編譯,但執行時會找不到對應的 .so ## ZShell 的話在 ~/.zshrc 加入下面幾行 ```bash= # Set custom gcc and g++ paths export PATH=~/.local/gcc-13.2.0/usr/local/bin/:$PATH # Set LD_LIBRARY_PATH if [[ -z $LD_LIBRARY_PATH ]]; then export LD_LIBRARY_PATH=~/.local/gcc-13.2.0/usr/local/lib/:~/.local/gcc-13.2.0/usr/local/lib64/ else export LD_LIBRARY_PATH=~/.local/gcc-13.2.0/usr/local/lib/:~/.local/gcc-13.2.0/usr/local/lib64/:$LD_LIBRARY_PATH fi ``` ## 附註 - 與系統的 gcc, g++ 還是略有不同,安裝在 user 端的 gcc, g++ 在編譯時需要手動鏈結 ```bash= [USERNAME@XXXXX ~/test]$ cat thread.cc #include <thread> // Need to add -lpthread when compiling int main() { std::thread([] {}).join(); return 0; } [USERNAME@XXXXX ~/test]$ /usr/bin/g++ -std=c++11 thread.cc # SUCCESS [USERNAME@XXXXX ~/test]$ ~/.local/gcc-13.2.0/usr/local/bin/g++ -std=c++11 thread.cc # FAILURE /tmp/ccsNMem6.o: In function 'std::thread::_M_thread_deps_never_run()': thread.cc:(.text._ZNSt6thread24_M_thread_deps_never_runEv[_ZNSt6thread24_M_thread_deps_never_runEv]+0x5): undefined reference to 'pthread_create' thread.cc:(.text._ZNSt6thread24_M_thread_deps_never_runEv[_ZNSt6thread24_M_thread_deps_never_runEv]+0xa): undefined reference to 'pthread_join' collect2: error: ld returned 1 exit status [USERNAME@XXXXX ~/test]$ ~/.local/gcc-13.2.0/usr/local/bin/g++ -std=c++11 thread.cc -lpthread # SUCCESS ``` - 安裝新版 gcc, g++ 後 原本的 pretty printing 會失效,需要新增 ~/.gdbinit 調用正確的 printers.py ```bash= python # python or python3 import sys sys.path.insert(0, '~/.local/gcc-13.2.0/usr/local/share/gcc-13.2.0/python') from libstdcxx.v6.printers import register_libstdcxx_printers register_libstdcxx_printers (None) end ``` ## 參考連結 1. [install gcc without root](https://www.jianshu.com/p/8d61bfa51f96) 2. [How to fix "export: command not found" in CentOS?](https://unix.stackexchange.com/questions/680370/how-to-fix-export-command-not-found-in-centos) 3. [Apache/2.4.29 (Trisquel_GNU/Linux) Server at ftp.gnu.org Port 443](https://ftp.gnu.org/gnu/gcc/) 4. [Installing GCC: Configuration](https://gcc.gnu.org/install/configure.html) 5. [Enable GDB Pretty Printing](https://stackoverflow.com/questions/4985414/how-to-enable-gdb-pretty-printing-for-c-stl-objects-in-eclipse-cdt)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up