How to install the GCC compiler

Source: Internet
Author: User

Currently, GCC can be used to compile programs in C/C ++, Fortran, Java, objc, Ada, and other languages. You can select the language to install. This article introduces the installation process of GCC by taking gcc4.1.2 installed on Redhat Linux as an example (the latest GCC version is not used because it is required during project development.

Before installation, a cc or GCC compiler must be available in the system, or the compiler on the system must be specified using the environment variable CC. If there is no compiler on the system, GCC 4.1.2 In the source code form cannot be installed. If this is the case, you can find a GCC software package in binary format such as RPM that suits your system on the Internet for installation and use. This article describes the installation process of the GCC software package provided in the form of source code. The software package itself and its installation process are also applicable to other Linux and UNIX systems.

On the System, the original GCC compiler may store command files such as GCC, library files, and header files in different directories of the system. In contrast, we recommend that you install a version of GCC in a separate directory. The advantage of doing so is that you can easily Delete the entire directory without needing it in the future (because GCC does not have the uninstall function ); the disadvantage is that you need to make some settings after installation to make the compiler work normally. This solution is used to install GCC 4.1.2 In this article. After the installation is complete, the GCC compiler of the earlier version can still be used, that is, a system can coexist and use multiple versions of the GCC compiler.

Follow the steps and setup options provided in this article to install a working New version of GCC compiler on the system even if GCC has not been installed before.

1. Download

You can find the download resources on the GCC website (http://gcc.gnu.org) or by searching online. The latest version of GCC is 4.2.1. Two types of files are available for download: gcc-4.1.2.tar.gzand gcc-4.1.2.tar.bz2. The compression format is different and the content is completely consistent. Download either of them.

2. Extract

Beibeigcc-4.1.2.tar.bz2 (the compressed file I downloaded) to/usr/local/src (depending on your preferences), according to the compression format, select one of the following methods to unpackage (the following "%" indicates a command line prompt ):

% Tar zxvf gcc-4.1.2.tar.gz
Or
% Bzcat gcc-4.1.2.tar.bz2 | tar xvf-

The newly generated gcc-4.1.2 directory is called the source directory, represented by $ {srcdir. In the future where $ {srcdir} appears, replace it with the actual path. Run the PWD command to view the current path.

Detailed gccinstallation instructions are provided in the $ {srcdir}/installobject directory. You can open index.html with a browser.

3. Create the target directory
 
The target directory (represented by $ {objdir}) is used to store the compilation result. GCC suggests that the compiled files should not be placed in the source directory $ {srcdir] (although this can be done). It is best to store the files in another directory separately, it cannot be a subdirectory of $ {srcdir.

For example, you can create a target directory named/usr/local/gcc-4.1.2 like this:

% Mkdir/usr/local/gcc-4.1.2
% Cd gcc-4.1.2

The following operations are mainly performed in the target directory $ {objdir.

4. Configuration
 
The purpose of the configuration is to determine where the GCC compiler is installed ($ {destdir}), languages supported, and other options. $ {Destdir} cannot be the same as the $ {objdir} or $ {srcdir} directory.

The configuration is completed by running configure under $ {srcdir. The command format is (replace $ {destdir} with your actual path }):

% $ {Srcdir}/configure -- prefix =$ {destdir} [other options]

For example, if you want to install GCC 4.1.2 to the/usr/local/gcc-4.1.2 directory, $ {destdir} represents this path.

On my machine, I configured it as follows:

% .. /Gcc-4.1.2/configure -- prefix =/usr/local/gcc-4.1.2 -- enable-threads = POSIX -- disable-checking -- enable -- long-long -- Host = i386-redhat-linux -- With-system-zlib -- enable-languages ages = C, c ++, Java

Install GCC in the/usr/local/gcc-4.1.2 directory and support the C/C ++ and Java languages. For other options, see the help instructions provided by GCC.

5. Compile

% Make

6. Install

Run the following command to copy the compiled library files to the $ {destdir} directory (administrator permissions may be required based on the path you set ):

% Make install

Now, the installation process of GCC 4.1.2 is complete.

6. other settings

All GCC 4.1.2 files, including command files (such as GCC and G ++) and library files, are stored in the $ {destdir} directory, for example, the command file is placed in the bin directory, the library file is under Lib, And the header file is under include. Because the directory where the command files and library files are located is not included in the corresponding search path, you must make appropriate settings before the compiler can find and use them smoothly.

6.1 GCC, G ++, and gcj settings

To use commands such as GCC 4.1.2, put $ {destdir}/bin in the environment variable path. I don't need to use this method, but use Symbolic connections. The advantage of this is that I can still use the old version of GCC compiler on the system.

First, check the path of the original GCC:

% Which gcc

On my system, the above command shows:/usr/bin/GCC. Therefore, the original GCC command is in the/usr/bin directory. We can separate the GCC, G ++, gcj, and other commands in GCC 4.1.2 into a symbolic connection under the/usr/bin directory:

% Cd/usr/bin
% Ln-S $ {destdir}/bin/GCC gcc412
% Ln-S $ {destdir}/bin/g ++ 412
% Ln-S $ {destdir}/bin/gcj gcj412

In this way, gcc412, G ++ 412, and gcj412 can be respectively used to call GCC, G ++, and gcj of GCC 4.1.2 to compile C, C ++, and Java programs. At the same time, you can still use GCC, G ++, and other commands in the earlier version of GCC compiler.

6.2 database path settings

Add the $ {destdir}/lib path to the environment variable LD_LIBRARY_PATH. For example, if GCC 4.1.2 is installed in the/usr/local/gcc-4.1.2 directory, in rh Linux, you can directly execute
% Export LD_LIBRARY_PATH =/usr/local/gcc-4.1.2/lib

It is best to add it to the system configuration file, so that you do not need to set this environment variable every time. In the file $ home/. bash_profile, add the following two sentences:
LD_LIBRARY_PATH =/usr/local/gcc-4.1.2/lib: $ LD_LIBRARY_PATH
Export LD_LIBRARY_PATH

Restart the system to take effect, or execute the command
% Source $ home/. bash_profile

7. Test
 
Use the new compilation commands (gcc412, G ++ 412, etc.) to compile your previous C and C ++ programs and check whether the newly installed GCC compiler works properly.

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.