Now many programmers are using GCC to better apply GCC. 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 describes how to install GCC in Linux by taking GCC4.1.2 installation in 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
Http://gcc.gnu.org on the GCC website) or you can search online to find the download resource. 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 is represented by $ {objdir}) to store the compilation result. GCC recommends that you do not store the compiled files in the source directory $ {srcdir]. You can also store the files in another directory, but not the subdirectories 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
Execute the following command to copy the compiled library files to the $ {destdir} directory, depending on the path you set, administrator permissions may be required ):
% Make install
Now, the installation process of GCC 4.1.2 is complete.
7. 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.
7.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.
7.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
8. 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.
After installing GCC in Linux, you can easily edit it.
- Linux Opera easily applies scim
- Simple Linux User Password
- How to obtain the file size in Linux
- Easily install Flash Media Server in Linux
- Describes how to configure the Clock Synchronization Service NTPD in Linux.