How to compile and install the gcc compiler in CentOS

Source: Internet
Author: User
Tags ftp gmp zip 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.gz
Cd 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.5
Cd gcc-build-4.8.5
../Configure -- prefix =/usr
Make & 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 -- version
Gcc (GCC) 4.8.5

G ++ -- version
G ++ (GCC) 4.8.5

Which gcc
/Usr/bin/gcc

Which 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.