Home  >  Article  >  Operation and Maintenance  >  How to install gcc under linux?

How to install gcc under linux?

藏色散人
藏色散人Original
2019-05-18 11:06:3648587browse

How to install gcc under linux?

1. Learn about gcc 

Currently, GCC can be used to compile programs in C/C, FORTRAN, JAVA, OBJC, ADA and other languages. You can choose to install supported languages ​​according to your needs. My own Linux is version 4.1.2, which does not support openMP, and I need to use openMP, so I installed a version 4.2 or above on the original basis.

Before installation, the system must have a cc or gcc compiler. If not, a higher version cannot be installed. gcc. If this is the case, you can find a GCC software package in binary form such as RPM that is suitable for your system on the Internet to install and use.

This article introduces the process of installing gcc from source code. I installed it in a separate directory. If you want to uninstall it in the future, just delete the directory. This article introduces Detailed process for installing version 4.3.4. Just follow the steps below to install it step by step.

2. Installation package that needs to be downloaded

2.1 Download gcc

The download URL is http://ftp.gnu.org/gnu/gcc/. You can download which version according to your needs. I chose version 4.3.4. There are gcc-4.3.4.tar.bz2 and gcc-4.3.4.tar.gz in it. Just choose any one. They are exactly the same. I chose gcc-4.3.4.tar.gz. You can also view Changes: http://gcc.gnu.org/gcc-4.5/changes.htm. You can just take a look at the changes compared to the previous version.

2.2 Download 3 dependency packages

One is mpc, one is gmp, and one is mpfr. The download addresses are: ftp://ftp.gnu.org/gnu/mpc/ mpc-1.0.2.tar.gz, ftp://ftp.gnu.org/gnu/gmp/gmp-5.0.1.tar.bz2, http://ftp.gnu.org/gnu/mpfr/mpfr- 3.1.2.tar.gz. The order of installation is: GMP, mpfr, mpc, and finally gcc. I installed gcc directly from the beginning, and the following message appeared during the configuration process:

configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+.Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify their locations.

prompts that before installing gcc, GMP 4.2 or above, MPFR 2.3.1 or above, MPC 0.8.0 or above must be installed. Version.

3. Installation steps

##3.1 Install GMP-5.0.1.

What I chose here is the GMP5.0.1 version. After decompressing gmp-5.0.1, get the source code directory and create a temporary compilation directory temp.

$ tar -jxvf gmp-5.0.1.tar.bz2
$ cd gmp-5.0.1
$ mkdir temp
$ cd temp

Start configuring the installation directory. After entering the temp directory, enter the following command:

$ ../configure --prefix=/usr/local/gmp-5.0.1
$ make
$ make install

In this way, the gmp installation is completed. The process of installing mpfr and mpc is similar, except that you have to add dependencies during installation

##3.2 Install mpfr

What I chose here is version 3.1.2 of mpfr. Unzip, create a temporary compilation directory temp, enter the temp directory

#

$ tar -zxvf mpfr-3.1.2.tar.gz 
$ cd mpfr-3.1.2
$ mkdir temp
$ cd temp
$ ../configure --prefix=/usr/local/mpfr-3.1.0 --with-gmp=/usr/local/gmp-5.0.1
$ make
$ make install
where --with=/usr/ local/gmp-5.0.1 is a dependency, /usr/local/gmp-5.0.1 is the installation directory of gmp

3.3 Install mpc

I chose the 1.0.2 version of mpc. Unzip it, create a temporary compilation directory temp, and enter the temp directory.

$ tar -zxvf mpc-1.0.2.tar.gz
$ cd mpc-1.0.2
$ mkdir temp
$ cd temp
$ ../configure --prefix=/usr/local/mpc-1.0.2 --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.0
$ make
$ make install
Remember the dependencies of the latter two items, which are the installation directories of your gmp and mpfr

##3.4 Install gcc

The three previously installed are all preparations for installing gcc. Unzip the gcc installation package

$ tar zxvf gcc-4.3.4.tar.gz 或者 $ tar jxvf gcc-4.5.1.tar.bz2

and the directory gcc-4.3.4 obtained is the source directory, represented by ${srcdir}. There are detailed installation procedures in the INSTALL directory in gcc-4.3.4. They are all in English, and the source directories inside are also represented by ${srcdir}. I unzipped it in the /usr/local/src/gcc-4.3.4 directory. Create the target directory

$ mkdir /usr/local/gcc-4.3
$ cd gcc-4.3

This is the target directory, which is the compiled directory, and all the following operations are performed in this directory. Use ${objdir}

Create the installation directory

$ mkdir /usr/local/gcc-4.3.4

This is where your gcc is installed. Represented by ${destdir}. When starting the configuration

$ ${srcdir}/configure --prefix=${destdir} [其它选项]

When installing, remember to replace ${srcdir} and ${destdir} with the real directory. Mine is written like this

$ /usr/local/src/gcc-4.3.4/configure --prefix=/usr/local/gcc-4.3.4 --enable-threads=posix --disable-checking --enable--long-long --enable-languages=c,c++,java --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.0 --with-mpc=/usr/local/mpc-1.0.2

记得加上后面三个依赖项,否则会出刚开始出现的错误的。然后安装

$ make
$ make install

安装的时候等挺长时间,差不多两个小时。

3.5 gcc、g++、gcj设置

要想使用GCC 4.3.4的gcc等命令,简单的方法就是把它的路径${destdir}/bin放在环境变量PATH中。我不用这种方式,而是用符号连接的方式实现,这样做的好处是我仍然可以使用系统上原来的旧版本的GCC编译器。

而原来gcc的路径是在usr/bin下。我们可以把gcc 4.3.4中的gcc、g++、gcj等命令在/usr/bin目录下分别做一个符号连接:

$ cd /usr/bin
$ ln -s /usr/local/gcc-4.3.4/bin/gcc gcc434
$ ln -s /usr/local/gcc-4.3.4/bin/g++ g++434
$ ln -s /usr/local/gcc-4.3.4/bin/gcj gcj434

这样,就可以分别使用gcc434、g++434、gcj434来调用GCC 4.1.2的gcc、g++、gcj完成对C、C++、JAVA程序的编译了。同时,仍然能够使用旧版本的GCC编译器中的gcc、g++等命令。

3.6 库路径的设置

将${destdir}/lib路径添加到环境变量LD_LIBRARY_PATH中,例如,如果GCC 4.3.4安装在/usr/local/gcc-4.3.4目录下,在RH Linux下可以直接在命令行上执行

$ export LD_LIBRARY_PATH=/usr/local/gcc-4.3.4/lib

最好添加到系统的配置文件中,这样就不必要每次都设置这个环境变量了,在文件$HOME/.bash_profile中添加下面两句:

LD_LIBRARY_PATH=:/usr/local/mpc-1.0.2/lib:/usr/local/gmp-5.0.1/lib:/usr/local/mpfr-3.1.2/lib:/usr/local/gcc-4.3.4/lib
export LD_LIBRARY_PATH

或者在/etc/bash_profile 下添加。

重启系统设置生效,或者执行命令

$ source $HOME/.bash_profile

或者:

$ source /etc/bash_profile

用新的编译命令(gcc412、g++412等)编译你以前的C、C++程序,检验新安装的GCC编译器是否能正常工作。

完成了Linux安装GCC,之后你就能轻松地编辑了。

相关学习推荐:linux视频教程

The above is the detailed content of How to install gcc under linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is gcc in linuxNext article:What is gcc in linux