Compile and install GCC in CentOS
We know that for GCC to install the default version through yum in CentOS, CentOS 5 is 4.1.2, CentOS 6 is 4.4.7, and CentOS 7 is 4.8.3.
Most of the time, compiling and installing software requires a later version of GCC. Otherwise, an error is reported.
So how to upgrade the GCC version?
First, check the GCC version number.
Currently, the latest version of GCC has reached 5.2, while CentOS 7 still uses 4.8. Therefore, based on compatibility considerations, I chose to upgrade to 4.8.5.
GCC Website: https://gcc.gnu.org
Next, we will start to compile and install GCC 4.8.5. Note that, before compiling and installing GCC, the system must first install the GCC and dependent libraries of the old version through yum.
If it is compiled in the x86_64 system, you also need to install libgcc. i686 glibc-devel.i686.
yum install -y gcc texinfo-tex flex zip libgcc.i686 glibc-devel.i686
Of course, if it is installed on CentOS 5 x86_64, the following command is used:
yum install -y gcc texinfo-tex flex zip libgcc.i386 glibc-devel.i386
At the same time, it should be noted that the GCC memory for compilation and installation is not less than 1 GB, the Swap is not less than 1 GB, and the hard disk is not less than 10 GB. Otherwise, an error may be reported and exited.
After the compilation and installation are complete, the directory gcc-4.8.5 will have as much as 5 GB.
1. Download source code
wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.5/gcc-4.8.5.tar.gz
2. Download the dependency package
The compilation and installation of GCC must depend on the MCM, mpfr, and gmp packages. Fortunately, the built-in scripts in the GCC source code can easily download dependency packages.
tar zxf gcc-4.8.5.tar.gzcd gcc-4.8.5./contrib/download_prerequisites
In this script, you can see that the version numbers of the dependent packages are mpc-0.8.1, mpfr-2.4.2, and gmp-4.3.2.
3. Compile and install
mkdir gcc-build-4.8.5cd gcc-build-4.8.5../configure --prefix=/usrmake && make install
To avoid multiple GCC versions in the system after installation, specify the directory for compilation and installation as/usr. If-prefix is not specified, it is installed under/usr/local by default.
GCC 4.8.5 only has 105 MB of source code, so it is foreseeable that the entire compilation process takes a long time (about 2 hours ).
4. view the version number
gcc --versiongcc (GCC) 4.8.5g++ --versiong++ (GCC) 4.8.5which gcc/usr/bin/gccwhich g++/usr/bin/g++
Note: the installation is successful in CentOS 6.7 x86_64.
5. Test Procedures
Cd ~
Create a main. cpp file with the following content:
#include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; return 0; }
Compile main. cpp and execute the following command:
g++ main.cpp -o main
Execute the generated file:
./Main
The output is as follows:
Hello world!
Conclusion
Run the rpm command to check the GCC package installation status:
Rpm-qa | grep gcc
The output is as follows:
Gcc-4.4.7-16.el6.x86_64
Libgcc-4.4.7-16.el6.x86_64
Libgcc-4.4.7-16.el6.i686
Gcc-c ++-4.4.7-16. el6.x86 _ 64
Therefore, after compiling and installing GCC through the above steps, please use yum update with caution or use yum to upgrade GCC commands.
Reprinted, please note: Qiu Shui Yi Bing» compile and install GCC under CentOS