Detailed Linux Installation gcc method

Source: Internet
Author: User
Tags bz2 posix systemtap unpack gfortran



Download: http://ftp.gnu.org/gnu/gcc/gcc-4.5.1/gcc-4.5.1.tar.bz2
Browse: HTTP://FTP.GNU.ORG/GNU/GCC/GCC-4.5.1/
View Changes:http://gcc.gnu.org/gcc-4.5/changes.htm



Now many programmers are using GCC, how to better apply gcc. Currently, GCC can be used to compile programs in C + +, FORTRAN, JAVA, OBJC, Ada and other languages, optionally installing the supported languages as required. This article describes the Linux installation GCC process as an example of Redhat Linux installation GCC4.1.2 (which is required during the project development process and is not in the latest GCC version).



Before installation, the system must have a compiler such as CC or GCC, and is available, or use the environment variable CC to specify the compiler on the system. If there is no compiler on the system, you cannot install GCC 4.1.2 in the form of source code. If this is the case, you can use the Internet to find a compatible with your system, such as RPM binary form of the GCC package to install. This article describes the installation process for the GCC packages provided in source code, and the package itself and its installation process apply to other Linux and UNIX systems as well.



The original GCC compiler on the system may be the same as the GCC and other command files, library files, header files, etc. are stored in the system in different directories. Unlike this, GCC now recommends that you install a version of GCC in a separate directory. The advantage of this is that it is easy to delete the entire directory when it is not needed in the future (because GCC does not have the uninstall feature); The disadvantage is that you do some setup work to make the compiler work properly after the installation is complete. In this article, you install GCC 4.1.2, and after the installation is complete, you can still use the earlier version of the GCC compiler, the GCC compiler that can exist on a system and use multiple versions simultaneously.



Following the steps and setup options provided in this article, you can install a new, working version of the GCC compiler on your system, even if you have not previously installed GCC.



1 Downloads



Download resources can be found on the GCC website (http://gcc.gnu.org) or via online search. Currently the latest version of GCC is 4.2.1. The files available for download generally have two forms: gcc-4.1.2.tar.gz and gcc-4.1.2.tar.bz2, except that the compression format is not the same, the content is identical, and one can be downloaded.



2. Unzip



Copy the gcc-4.1.2.tar.bz2 (i downloaded the compressed file) to/USR/LOCAL/SRC (choose according to your preference), according to the compression format, select the appropriate way to unpack (the following "%" means 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 and is represented by ${srcdir}. In the future where ${srcdir} should appear, replace it with a real path. Use the PWD command to view the current path.



Detailed GCC installation instructions are available in the ${srcdir}/install directory and can be opened index.html read by your browser.



3. Set up the target directory



The target directory (denoted by ${objdir}) is where the compilation results are stored. GCC recommends that the compiled file not be placed in the source directory ${srcdir] (although this can be done), preferably in a separate directory, and cannot be a subdirectory of ${srcdir}.



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



% mkdir/usr/local/gcc-4.1.2



% CD gcc-4.1.2



The following operations are performed mainly under the target directory ${objdir}. (Otherwise there will be an error, explained later)



4. Configuration



The purpose of the configuration is to decide where to install the GCC compiler (${destdir}), what languages are supported, and specify some other options. Where ${destdir} cannot be the same as the ${objdir} or ${srcdir} directory.



The configuration is done by executing the Configure under ${srcdir}. Its command format is (remember to replace ${destdir} with your real path):



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



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



On my machine, this is how I configured it:



% .. /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=c,c++,java



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



5. Compiling



% make



6. Installation



Execute the following command to copy the compiled library files to the ${destdir} directory (depending on the path you set, you may need Administrator privileges):



% make Install



At this point, the GCC 4.1.2 installation process is complete.



7. Other Settings



GCC 4.1.2 All files, including command files (such as GCC, g++), library files, etc. are stored in the ${destdir} directory, such as command files placed in the bin directory, library files under Lib, the header file in include. Because the directory in which the command and library files are located is not included in the appropriate search path, the compiler must be properly set up to successfully locate and use them.



7.1 Settings for GCC, g++, GCJ



The simple way to use the GCC 4.1.2 command is to put its path ${destdir}/bin in the environment variable path. I don't use this approach, but I do it in symbolic connection, and the advantage is that I still have the old version of the GCC compiler on the system.



First, look at the path where the original GCC is located:



% 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 make the GCC, g++, GCJ and other commands in the GCC 4.1.2 a symbolic connection in the/usr/bin directory:



% Cd/usr/bin



% ln-s ${DESTDIR}/BIN/GCC gcc412



% ln-s ${destdir}/bin/g++ g++412



% ln-s ${DESTDIR}/BIN/GCJ gcj412



In this way, you can use gcc412, g++412, gcj412 to invoke GCC 4.1.2 gcc, g++, GCJ to complete the C, C + +, Java program compilation. At the same time, you can still use the GCC, g++, and other commands in the old version of the GCC compiler.



(Cool, I feel great!!) 1)



7.2 Setting of the library path



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, it can be executed directly on the command line under RH Linux



% Export Ld_library_path=/usr/local/gcc-4.1.2/lib



It is best to add to the system configuration file, so that you do not have to set this environment variable every time, add the following two sentences in the file $home/.bash_profile:



Ld_library_path=/usr/local/gcc-4.1.2/lib: $LD _library_path



Export Ld_library_path



Restart system settings to take effect or execute commands



% Source $HOME/.bash_profile



Compile your previous C and C + + programs with the new Compile command (gcc412, g++412, etc.) to verify that the newly installed GCC compiler is working properly.



Complete the Linux installation of GCC and you will be able to edit it easily.



From:os.51cto.com/art/200912/168804.htm



Installing the gcc-4.0.1 method under Rhlinux is relatively straightforward, but some aspects of the installation process need to be noted, otherwise, the installation may not be successful, or installation error. The specific installation process is as follows:



First, download and unzip the GCC RPM package to the source directory (e.g./opt/gcc-4.0.1)



1. Unzip the RPM package:



[Email protected]]# tar xjvf gcc-4.0.1.tar.bz2 (Generate source directory/opt/gcc-4.0.1 after decompression)



2. Create the installation target directory:



[Email protected] opt]# MKDIR/USR/LOCAL/GCC-4.0.1/



3. Enter the installation target directory:



[[email protected] opt]# CD/USR/LOCAL/GCC-4.0.1/(This step is important, when you configure the installation file, you need to execute the Configure command in the target directory)



[Email protected] opt]# pwd



/usr/local/gcc-4.0.1



4. Configure the installation file:



[Email protected]nux gcc-4.0.1]#/opt/gcc-4.0.1/configure--PREFIX=/USR/LOCAL/GCC-4.0.1/(This step is important and needs to be installed in the target directory, Execute the Configure command in the source directory/OPT/GCC-4.0.1/, configure GCC to be installed to the target directory/USR/LOCAL/GCC-4.0.1/)



Creating cache./config.cache



Checking host system Type ... I686-pc-linux-gnu



5. Compile the installation file:



[Email protected] gcc-4.0.1]# pwd



/usr/local/gcc-4.0.1



[[email protected] gcc-4.0.1]# make (compile in target directory)



6. Installing GCC:



[Email protected] gcc-4.0.1]# pwd



/usr/local/gcc-4.0.1



[[email protected] gcc-4.0.1]# make install (perform installation in target directory)



If there are no errors in the steps and commands during the installation process, you will certainly be able to install successfully.






---------------------------------------------------------------------





First of all, now the newest package gcc 4.2.bz2, general everywhere under, Linux download speed is very full, more than 20 k, very disgusting, I like the download speed of thunder, download under Windows, about 42M, download speed 2 to 4M, back to Linux, Mount, NTFS format my, specifically don't say, say install!

Into the mounted directory, first the CP Gcc4.2.bz2/azuo,

Cd/azuo,

TAR-XVF GCC 4.2.bz2,

Get gcc-4.2,

CD gcc-4.2, create a folder in the/usr directory is now to store the new GCC directory,

Mkdir/usr/gcc4

./configure–prefix=/usr/gcc4

Enter, there will be configuration information, as long as not an error can be,

Make, this process for a long time, because I did not set some specific options, so all the components are almost compiled again, my Computer cpu:amd 3200+x2, 1.5g of memory, about 1.5 hours.

By this time, there is not anything under the/USR/GCC4, the compilation process is happening in the source folder, let's take another action, all the things to be used will be configured to the target folder,

make install;

It's not a short process, just wait.

Everything has been done after the use of the latest GCC, you can see, in the/usr/gcc4/bin under the gcc,g++, and so on some things, can be used, write two programs:

AA.C:

1 #include

2

3 int main (void) {

4 printf ("ad");

5 return 1;

6}

Gcc-o AA AA.C

Execution./aa

The above is C, and the following is C + +:

A.cpp:

1 #include

2 using namespace Std;

3 int main (void) {

4 cout << "ok!" << Endl;

5 return 1;

6}

g++-O a a.cpp

Execution is./A

When everything is OK, you can get the latest GCC tools to replace the original tools.

Look at what the original GCC version is, and we'll uninstall it well:

[Email protected] bin]# Rpm-qa gcc

Gcc-4.1.2-27.fc7

[Email protected] bin]# rpm-e GCC-4.1.2-27.FC7

error:failed dependencies:

GCC is needed by (installed) systemtap-0.5.13-1.fc7.i386

GCC = 4.1.2-27.fc7 is needed by (installed) gcc-c++-4.1.2-27.fc7.i386

GCC = 4.1.2-27.fc7 is needed by (installed) gcc-gfortran-4.1.2-27.fc7. I386

[Email protected] bin]# rpm-e gcc-c++-4.1.2-27.fc7.i386

[Email protected] bin]# rpm-e gcc-gfortran-4.1.2-27.fc7.i386

[Email protected] bin]# rpm-e GCC-4.1.2-27.FC7

error:failed dependencies:

GCC is needed by (installed) systemtap-0.5.13-1.fc7.i386

[Email protected] bin]# g++

bash:g++: Command not found

Uninstall succeeded

[Email protected] bin]# gcc

GCC: No input file, visible gcc in

[Email protected] bin]# rpm-e systemtap-0.5.13-1.fc7.i386

[Email protected] bin]# gcc

GCC: no input file

[Email protected] bin]# rpm-e GCC-4.1.2-27.FC7

[Email protected] bin]# gcc

BASH:/USR/LIB/CCACHE/GCC: no file or directory

The final uninstall succeeded!

This time, to notice, my gcc in the/usr/bin below, in the/usr/lib/ccache This directory also has, respectively, in both below to establish a link:

[Email protected] bin]# ln-s/usr/gcc4/bin/g++ g++

[Email protected] bin]# g++

g++: No input file

Visible g++ has been installed, can be used.

Then there is GCC:

[Email protected] bin]# ln-s/USR/GCC4/BIN/GCC gcc

[Email protected] bin]# gcc

BASH:/USR/LIB/CCACHE/GCC: no file or directory

[Email protected] bin]#./GCC

GCC: no input file

It is also a link to another directory to create a GCC:

[Email protected] lib]# ln-s/USR/GCC4/BIN/GCC/USR/LIB/CCACHE/GCC

[Email protected] lib]# gcc

GCC: no input file

So far, GCC and g++ have been established, can be used, and finally the original package and decompression files can be deleted to save hard disk space!


-------------------------------------------------------------------------------------------------------


Here's how it installs in the Red Hat Linux 9.0 compiler to the GCC 3.2.2 environment.
A Determine the installation environment
This installation method applies to the Red Hat Linux 9.0 operating system, with the GCC compiler being the GCC 3.2.2 version. Other Linux operating systems or other versions of the GCC installation process may differ in some detail.
Note: Because some libraries may have different definitions under different versions of the GCC compiler, first determine the native GCC compiler version. The way to determine this is to go to the command-line Input command gcc–v The result 1.1 shows:




Figure 1.1
It can be seen that the GCC version of this machine is GCC 3.2.2, according to some introduction on the Simplescalar website, the development tool of the simulator may be around GCC2.7, which is closer to GCC 3.2.2, so it is estimated that the installation process will be more smooth.
Two. Get the installation package
The installation package that completes the installation process can be

Http://www.simplescalar.com
Download below, the installation package required for this installation consists of the following three


Set up the installation directory, unzip the installation package
To set up the installation directory as/root/simplescalar, copy the installation package to the installation directory, the entire process is as follows:


Decompression, the command is TAR–ZXVF, and after the compression is complete with the rm*.tgz command to delete the package, the entire process input command as follows:
TAR–ZXVF simplesim-3v0d.tgz
TAR–ZXVF simpletools-2v0.tgz
TAR–ZXVF simpleutils-2v0.tgz
Rm*.tgz
Add compression to get the following seven folders:


Four. Installing binutils2.5.2
First use the Configure command to configure the installation environment and parameters of the program, generate the makefile file, the whole process is as follows:



Note: The parameter meaning of the Configure command indicates
-host: Configuring the installation Environment
-target: Configuring into Littleendian mode
-with-gnu-as loading Assembler
-WITH-GNU-LD Loading the linker
-prefix setting up the installation directory
At this point, you encounter two errors, as follows:



From the error description, you can estimate that the error came from the dummy.c file under the Libiberty folder. Some online posts say that the function definitions in the macro Functions.def file defined in the dummy.c are inconsistent with the declaration, and they can be compiled by changing them to the same. But in doing so, continuing to compile will still encounter many errors. Here I try out the best way is to delete all the contents of the Dummy.c file (will be dummy.c into empty pieces) and then make again, this time not to report any errors, compile one pass!
Run the Make install command, and the binutils-2.5.2 installation is successful!
Five. Installing Simplescalar
Simplescalar is the simplest installation process, run the following command to complete the installation!



Six. Installing gcc-2.6.3
After installing the Simplescalar in the installation folder/root/simplescalar can find a folder named Bin, which contains some of the simplescalar comes with tools, such as Link tool, the file content is as follows:



Visible, there is no C compiler tool GCC, so also need to install gcc2.6.3 as Simplescalar's built-in compiler tool, the following describes the installation method.
First configure the installation environment to generate the makefile file, as shown in.



After the makefile file is generated, run the Make command, and the following error occurs:



By the error report, this is due to inconsistent definition of sys_errlist, open cccp.c file found its 194 lines near the following code, 194th behavior extern char *sys_errlist[]. Obviously to eliminate the conflict, just change the branch direction of the macro compilation, so that it does not go this branch. Try to precede this code with a # 175 line with a # define Bsd4_4, modify and make again, the error is corrected, and the next error is encountered.


The second error is that some constants in the sdbout.c file are not defined.



So the first estimate is the problem with the header file, open sdbout.c found that its included header file has the following several.

You can be sure that the cause of the error is the problem with the branch of the macro compilation (Take a different macro compilation branch, which may contain syms.h or gsyms.h). After several attempts, it was found that adding the #undef USG to the front solves the error (that is, the gsyms.h is included).
To continue make, a third error occurs, as follows:


This error, like the first one, is due to the sys_errlist definition conflict, so go to the gcc.c file and add a # define Bsd4_4 before 172 lines (add 167 lines here).
To continue make, a fourth error occurred.


As before, you can add a # define BSD4_4 before the 90th line of code in the G++.c file (plus 85 lines).
To continue make, a fifth error occurred.


Here is the hint is the cp/g++.c file in the No. 213 line of sys_errlist changed to Strerror or Strerror_r, but after the change will be reported strerror undefined error, Internet search for a long time did not find the two definitions of the header file name, Finally, according to the function name feel this code (PFATAL_WITH_NAME) function may be to get the wrong name, delete its estimate has little impact on the work, so simply empty the function, empty and then run make, sure enough, do not report any errors, compile through!
Run make INSTALL,GCC 2.6.3 installation is successful!
Return to Simplescalar, go to the Bin folder, you can find that there is a SSLITTLE-NA-SSTRIX-GCC file, the file is the built-in C compiler simplescalar.


Run further./sslittle-na-sstrix-gcc–v tested the built-in compiler version for GCC 2.6.3.


So far, the entire installation process is complete and tested below.
Seven. Testing
To test whether the simplescalar is running smoothly, we test it, and the test program still uses the most classic Hello world! program, the program is as follows:
#include
Main ()
{
printf ("Hello world!\n");
return 0;
}
After editing the program to save it in the/root/simplescalar folder, the file name is hello.c, with the newly installed simplescalar built-in compiler, compile the method as follows.




Eight concluding remarks
This installation method applies to the Red Hat Linux 9.0 operating system, with the GCC compiler being the GCC 3.2.2 version. Other Linux operating systems or other versions of the GCC installation process may differ in detail, mainly due to the fact that the higher version of GCC does not have some library files that are compatible with the lower version, and that the C standard that follows may be somewhat different. If GCC on the machine happens to be in the lower version, an estimate may be compiled once!
-----------------------------------------------------------------------------------------------------


1. Download
On the GCC website (
http://gcc.gnu.org/
) or search through the Internet to find the download resources. Current GC
The latest version of C is 3.4.0. There are generally two types of files available for download: gcc-3.4.0.tar.gz and gcc-3.4.0.tar.bz2,
Just the compression format is not the same, the content is exactly the same, download one of them.
2. Unzip
According to the compression format, select one of the following ways to unpack (the "%" below indicates the command line prompt):
% tar xzvf gcc-3.4.0.tar.gz
Or
% Bzcat gcc-3.4.0.tar.bz2 | Tar xvf-
The newly generated gcc-3.4.0 directory is known as the source directory and is represented by ${srcdir}. After the appearance of ${srcdir
Place, you should replace him with a real path. You can view the current path with the PWD command.
Detailed GCC installation instructions are available in the ${srcdir}/install directory and can be opened index.html read by your browser.
3. Set up the target directory
The target directory (denoted by ${objdir}) is where the compilation results are stored. GCC recommends that the compiled file do not
In the source directory ${srcdir] (although this can be done), the best standalone is stored in another directory, and does not
Can be a subdirectory of ${srcdir}.
For example, to create a gcc-build called
The target directory (and source directory ${srcdir} is the sibling directory):
% mkdir Gcc-build
% CD Gcc-build
The following operations are performed mainly under the target directory ${objdir}.
4. Settings
The purpose of the setting is to decide where to install the GCC compiler (${destdir}), what languages are supported and
Some other options. Where ${destdir} cannot be the same as the ${objdir} or ${srcdir} directory.
The settings are done by executing the Configure under ${srcdir}. Its command format is (remember to use your true path
Replacement ${destdir}):
% ${srcdir}/configure--prefix=${destdir} [other options]
For example, if you want to add GCC
3.4.0 is installed in the/usr/local/gcc-3.4.0 directory, ${destdir} represents this path.
On my machine, this is how I set it up:
% .. /gcc-3.4.0/configure--prefix=/usr/local/gcc-3.4.0
--enable-threads=posix--disable-checking--enable--long-long
--host=i386-redhat-linux--with-system-zlib--enable-languages=c,c++,java
Install GCC in the/usr/local/gcc-3.4.0 directory, support C + + and Java languages, other options see GCC
A description of the help provided.
5. Compiling
% make
It's a long process. On my Machine (p4-1.6), this process took more than 50 minutes.
6. Installation
Execute the following command to copy the compiled library file into the ${destdir} directory (depending on the path you set, you can
can require Administrator privileges):
% make Install
At this point, the GCC 3.4.0 installation process is complete.
6. Other Settings
Gcc
3.4.0 all files, including command files (such as GCC, g++), library files, etc. are stored in the ${destdir} directory, respectively.
Put, such as command files in the bin directory, library files under Lib, header files in include. Because the command file and
The directory where the library file resides is not included in the corresponding search path, so it must be set appropriately after the compiler
Able to find and use them smoothly.
6.1 Settings for GCC, g++, GCJ
To use GCC
3.4.0 the GCC command, the simple way is to put his path ${destdir}/bin in the environment variable path. I don't
In this way, it is done in a symbolic connection, and the advantage is that I can still use the original old
Version of the GCC compiler.
First, look at the path where the original GCC is located:
% which GCC
On my system, the above command shows:/USR/BIN/GCC. Therefore, the original GCC command is in the/usr/bin directory
Under We can put GCC
The GCC, g++, GCJ and other commands in 3.4.0 make a symbolic connection in the/usr/bin directory:
% Cd/usr/bin
% ln-s ${DESTDIR}/BIN/GCC gcc34
% ln-s ${destdir}/bin/g++ g++34
% ln-s ${DESTDIR}/BIN/GCJ gcj34
This allows you to invoke GCC using GCC34, g++34, and gcj34, respectively.
3.4.0 's gcc, g++, and GCJ completed the compilation of C, C + +, and Java programs. At the same time, the old version of the GC can still be used
The C compiler for GCC, g++, and other commands.
6.2 Setting of the library path
Add the ${destdir}/lib path to the environment variable Ld_library_path, which is best added to the system's setup text
So that you do not have to set this environment variable every time.
For example, if GCC 3.4.0 is installed in the/usr/local/gcc-3.4.0 directory, the RH
Linux can be executed directly on the command line or add the following sentence to the file/etc/profile:
Setenv ld_library_path/usr/local/gcc-3.4.0/lib: $LD _library_path
7. Testing
Compile your previous C and C + + programs with new compile commands (GCC34, g++34, etc.) to verify the newly installed GCC compilation
The device will work properly.
8. ${srcdir} and ${objdir} directories can be deleted or retained as needed.


Detailed Linux Installation gcc method


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.