Installing and using Ubuntu under GCC

Source: Internet
Author: User
Tags bz2 error code gmp mkdir

Installing GCC under Ubuntu is a bit different from some other Linux systems.

First, installation method one:

sudo apt-get  build-dep  gcc
Method Two:
sudo apt-get  install  build-essential
After installation, you can perform the following commands to view the version.
GCC--version
The output is as follows:
GCC (GCC) 4.2.3 (UBUNTU4.2.3-2UBUNTU7)
Copyright (C) 2007freesoftwarefoundation,inc.

Second, compile

compilation uses the GCC command. To learn first, you need to familiarize yourself with the use of GCC commands.

The GCC command provides a lot of command options.
I. Common COMPILE command options
Suppose the source program file is named TEST.c.

1.  no option to compile links
Usage: #gcc  test.c
Role: preprocess, assemble, compile, and link test.c to form an executable file. The output file is not specified here, and the default output is a.out.

2.  Options  -o
Usage: #gcc  test.c -o test
: preprocessing, assembling, compiling, and linking test.c to form the executable test. The-o option is used to specify the file name of the output file.

3.  option  -e
Usage: #gcc  -e test.c -o test.i
: test.c preprocessing output test.i file.

4.  option  -s
Usage: #gcc  -s test.i 
Role: Assembles the preprocessed output file test.i into a test.s file.

5.  Options  -c
Usage: #gcc  -c test.s
: Compiles output files Test.s compiled outputs TEST.O files.

6.  no option links
Usage: #gcc  test.o -o test
Role: Link the compiled output file TEST.O to the final executable test.

7.  Option-o
Usage: #gcc  -o1 test.c -o test
Role: Compile the program with compile-level 1. The higher the level, the better the optimization effect, but the longer the compilation time.

two. Multi-source file compilation method

If you have more than one source file, there are basically two ways to compile it:
[Suppose there are two source files for TEST.c and TESTFUN.C]

1. Compiling multiple files together
Usage: #gcc testfun.c test.c-o Test
Role: TESTFUN.C and TEST.c are compiled and linked into Test executables.

2. Compile each source file separately, then link to the target file for post-compilation output.

Usage:
#gcc-C TESTFUN.C//TESTFUN.C compiled into TESTFUN.O
#gcc-C test.c//test.c compiled into TEST.O
#gcc-o testfun.o test.o-o Test//link TESTFUN.O and TEST.O to test

The above two methods compare, the first method compiles all the files need to recompile, and the second method can only recompile modified files, unmodified files do not have to recompile.

first, what is GCC

The GNU C Compiler, a Linux-based system, is one of the GNU's representative works, which is a powerful and superior performance multi-platform compiler. GCC is a super compiler that can compile executable programs on a variety of hardware platforms, and its execution efficiency is 20%~30% higher than the average compiler.

The GCC compiler compiles and connects the C, C + + language source, sink, and target programs into an executable file, and if the name of the executable is not given, GCC generates a file named A.out. In Linux systems, executables do not have a uniform suffix, and the system distinguishes between executables and non-executable files from the properties of a file.Ii. Some of the Convention rules followed by GCCThe earlier mentioned is the cheap suffix problem, and GCC uses the suffix to differentiate the input file categories, let us introduce the partial convention rules that GCC follows: c is the suffix file, C language source code file;. A is a suffix file, which is the archive file consisting of the target file;
. C or. cc or. cxx for the suffix file, is a C + + source code file;. h is the suffix of the file, is the program contains the header file;. I is the suffix of the file, is already preprocessed C source code files;. II is the suffix file, which is a file that has been preprocessed C + + source code; is the objective-c source code file; O is the suffix of the file, is the compiled target file,. s is the suffix of the file, is the assembly language source code files;. S is the suffix of the file, is a precompiled assembly language source code files.III. implementation process of GCCAlthough we call GCC a C compiler, the process of using GCC to generate executables from C language source code files is not just a compilation process, but a four interrelated steps: preprocessing (also known as precompilation, preprocessing), compilation (compilation), assembly (Assembly) and connection (linking). The command GCC first invokes the CPP for preprocessing, and during the preprocessing process, the files contained in the source code file (include), precompiled statements (such as macro definition define, etc.) are analyzed. The CC1 is then called to compile, which generates a target file with an. o suffix based on the input file. Assembly process is for assembly language steps, call as to work, generally speaking,. S is the suffix of the assembly language source code files and assembly,. s suffix assembly language files are precompiled and compiled to generate a target file with an. o suffix. When all the target files are generated, GCC invokes the LD to complete the final critical work, which is the connection. During the connection phase, all the target files are placed in the appropriate place in the executable program, and the library functions that the program calls to are also connected to the appropriate location from the respective archive.Iv. Basic uses and options for GCCWhen using the GCC compiler, we must give a list of necessary invocation parameters and file names. The GCC compiler has more than 100 call parameters, most of which we may not be able to use at all, here only the most basic and most commonly used parameters of the GCC most basic usage is: gcc[options] [filenames], where options is the compiler required parameters, Filenames gives the associated file name.v. Parameters options for GCC-C, which compiles only, does not connect as an executable, and the compiler only generates. O is the target file of the suffix, usually used to compile a subroutine file that does not contain the main program. -O output_filename, determine that the output file name is Output_filename, and that the name cannot be the same as the source file. If this option is not given, GCC will give the default executable file a.out. -G, the symbolic information necessary to produce the symbolic debugging tool (GNU GDB), to debug the source code, we must add this option. -O, the program is optimized to compile, connect, with this option, the entire source code will be compiled, connected process optimization processing, so that the resulting executable file execution efficiency can be improved, but the compilation, connection speed is correspondingly slower. -o2, better than-O to optimize the compilation, connection, of course, the entire compilation, the connection process will be slower.
-idirname, adding the directory indicated by DirName to the program header file directory list is the parameter used during the precompilation process.Vi. installation method of GCCOperating environment: VMware? Workstation 7.1.2 build-301548
Linux version: Red Hat Enterprise Linux 5.5
Kernel version: Linux kernel 2.6.18-194.el5 #1 SMP Tue Mar 21:52:43 EDT (i686 i686 i386 gnu/linux)
Note: Because of the WordPress escape problem: May lead to the following--translation into------------Don't copy the code in the experiment.
Write your own hand, such as two before PREFIX,WITH-GMP,WITH-MPFR,WITH-MPC-

1. Download the gcc-4.6.1 installation package
GCC versions browse Address: http://ftp.gnu.org/gnu/gcc/
gcc-4.6.1 Browse Address: HTTP://FTP.GNU.ORG/GNU/GCC/GCC-4.6.1/
gcc-4.6.1 Download Address: http://ftp.gnu.org/gnu/gcc/gcc-4.6.1/gcc-4.6.1.tar.bz2
2. Place the gcc-4.6.1.tar.bz2 in the/opt folder to extract
[Root@rekfan.com ~]# cd/opt
[Root@rekfan.com opt]# tar xjvf gcc-4.6.1.tar.bz2
3. Create the installation directory
[Root@rekfan.com opt]# MKDIR/USR/LOCAL/GCC-4.6.1/
4. Go to the installation directory
[Root@rekfan.com opt]# CD/USR/LOCAL/GCC-4.6.1/
5. Configure the installation files
[Root@rekfan.com gcc-4.6.1]#/opt/gcc-4.6.1/configure--prefix=/usr/local/gcc-4.6.1
(Execute the Configure command in the source directory/OPT/GCC-4.6.1/, configure GCC to be installed in the target directory/USR/LOCAL/GCC-4.6.1/, where the –prefix option represents where the library is to be installed, I am installed in/usr/local/ gcc-4.6.1 directory, this option will be used for subsequent installations)

If you perform this step, the following error occurs:
----------------------------------------------------------------------------------
[Root@rekfan.com gcc-4.6.1]#/opt/gcc-4.6.1/configure--PREFIX=/USR/LOCAL/GCC-4.6.1/
Checking build system Type ... I686-pc-linux-gnu
Checking host system Type ... I686-pc-linux-gnu
Checking target system Type ... I686-pc-linux-gnu
Checking for a bsd-compatible install .../usr/bin/install-c
Checking whether LN works ... yes
Checking whether Ln-s works ... yes
Checking for a sed this does not truncate output .../bin/sed
Checking for gawk ... gawk
Checking for gcc ... no
Checking for cc ... no
Checking for cl.exe ... no
Configure:error:in '/usr/local/gcc-4.6.1 ':
Configure:error:no acceptable C compiler found in $PATH

See ' Config.log ' for more details.
----------------------------------------------------------------------------------
The above error indicates that C's compiler was not found in $path
If the system has a GCC compiler, set the environment variable
Like what:
Export path= $PATH:/usr/local/bin
If not found then install the GCC low version RPM package first
Insert the Linux software disk (usually the first or second), mount the disc, and install it in the following order:

[Root@rekfan.com opt]# RPM-IVH cpp-4.1.2-48.el5.i386.rpm
[Root@rekfan.com opt]# RPM-IVH kernel-headers-2.6.18-194.el5.i386.rpm
[Root@rekfan.com opt]# RPM-IVH glibc-headers-2.5-49.i386.rpm
[Root@rekfan.com opt]# RPM-IVH glibc-devel-2.5-49.i386.rpm
[Root@rekfan.com opt]# RPM-IVH libgomp-4.4.0-6.el5.i386.rpm
[Root@rekfan.com opt]# RPM-IVH gcc-4.1.2-48.el5.i386.rpm

Repeat step 5 with the new error, and the following is a partial error code. If there are no errors, skip directly to step 7!
----------------------------------------------------------------------------------
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.
----------------------------------------------------------------------------------

Error description to install GCC requires GMP, MPFR, MPC these three libraries, you can download the corresponding compression package from ftp://gcc.gnu.org/pub/gcc/infrastructure/. Because MPFR relies on GMP, and MPC relies on GMP and MPFR, so we should install GMP first, next MPFR, the last is MPC. Here are three libraries I use the version of gmp4.3.2,mpfr2.4.2 and mpc0.8.1, respectively, placed under the/opt folder.
①. Installing gmp4.3.2
[Root@rekfan.com opt]# tar jxvf gmp-4.3.2.tar.bz2
[Root@rekfan.com opt]# mkdir/usr/local/gmp-4.3.2
[Root@rekfan.com opt]# cd/usr/local/gmp-4.3.2
[Root@rekfan.com gmp-4.3.2]#/opt/gmp-4.3.2/configure--prefix=/usr/local/gmp-4.3.2
[Root@rekfan.com gmp-4.3.2]# make (Compile)
[Root@rekfan.com gmp-4.3.2]# make install (perform installation)
② installation mpfr2.4.2
[Root@rekfan.com opt]# tar jxvf mpfr2.4.2.tar.bz2
[Root@rekfan.com opt]# mkdir/usr/local/mpfr-2.4.2
[Root@rekfan.com opt]# cd/usr/local/mpfr-2.4.2
[root@rekfan.com mpfr-2.4.2]#/opt/mpfr-2.4.2/configure--prefix=/usr/local/mpfr-2.4.2--with-gmp=/usr/local/ gmp-4.3.2
(Note that the dependency options are added when configuring)
[Root@rekfan.com mpfr-2.4.2]# make
[Root@rekfan.com mpfr-2.4.2]# make install
③ installation mpc0.8.1
[Root@rekfan.com opt]# tar jxvf gmpc0.8.1.tar.bz2
[Root@rekfan.com opt]# mkdir/usr/local/mpc-0.8.1
[Root@rekfan.com opt]# cd/usr/local/mpc-0.8.1
[root@rekfan.com mpc-0.8.1]#/opt/mpc-0.8.1/configure--prefix=/usr/local/mpc-0.8.1--with-gmp=/usr/local/ gmp-4.3.2--with-mpfr=/usr/local/mpfr-2.4.2
[Root@rekfan.com mpc-0.8.1]# make
[Root@rekfan.com mpc-0.8.1]# make install
④ Installing the GCC configuration installation option again
[Root@rekfan.com mpc-0.8.1]# cd/usr/local/gcc-4.6.1
[Root@rekfan.com gcc-4.6.1]#/opt/gcc-4.6.1/configure--prefix=/usr/local/gcc-4.6.1-enable-threads=posix- Disable-checking-disable-multilib-enable-languages=c,c++--with-gmp=/usr/local/gmp-4.3.2--with-mpfr=/usr/local/ mpfr-2.4.2--with-mpc=/usr/local/mpc-0.8.1

6. Compiling the installation files
[Root@rekfan.com gcc-4.6.1]# make
It will take about 1 hours to drink a cup of coffee and wait.
One hours later ...
The second error appears:

Open/usr/local/gcc-4.6.1/i686-pc-linux-gnu/libgcc/config.log
ctrl+f find error found below
/--------------------- -------------------------------------------------------------
/usr/local/gcc-4.6.1/./gcc/cc1:error while Loading shared Libraries:libmpc.so.2:cannot Open Shared object file:no such file or directory
configure:3058: $? = 1
----------------------------------------------------------------------------------/
found a workaround on the web, To add an environment variable Ld_library_path to indicate the location of the previous three libraries, type the following command:
[root@rekfan.com gcc-4.6.1]# export ld_library_path= $LD _ Library_path:/usr/local/mpc-0.8.1/lib:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-2.4.2/lib
Perform step 6→ again
about three hours later ··· The
finally compiles successfully, the effect diagram is entered:

7. Perform the installation of GCC
[Root@rekfan.com gcc-4.6.1]# make install
[Root@rekfan.com gcc-4.6.1]#
If not, then the installation should be successful now, but it is still not possible to use the new version of GCC because the new version of GCC has not been added to the command search path.
8. Create a soft link
[root@rekfan.com/]# sudo ln-s/usr/local/gcc-4.6.1/bin/gcc gcc461
[root@rekfan.com/]# sudo ln-s/usr/local/gcc-4.6.1/bin/g++ g++461
9. Add Environment variables
Open the BASHRC file in the/etc directory to add the following code:
ld_library_path=:/usr/local/mpc-0.8.1/lib:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-2.4.2/lib:/usr/local/ Gcc-4.6.1/lib

Export Ld_library_path

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.