Several minor problems with CentOS compiler HelloWorld

Source: Internet
Author: User
Tags gmp

1.GCC Use
When using GCC to compile a program, the compilation process can be subdivided into four phases:
Pretreatment (pre-processing)
Compilation (compiling) compilation (assembling)
Link (linking).
For example:
#include
int main (void)
{

printf ("Hello World, Linux programming!\n");

return 0;
}
Then execute the following command to compile and run the program:
# gcc Hello.c-o Hello
#
./hello
Hello World, Linux programming!

GCC needs to invoke the preprocessor CPP, which is responsible for expanding the macros defined in the source file and inserting the contents of the "#include" statement into it, and then GCC will call CCL and as to compile the processed source code into the target, and finally, GCC invokes the linker LD, Link the generated target code into an executable program.

The first step is to precompile, and using the-e parameter allows GCC to stop the compilation process after the preprocessing is finished:
# GCC-E Hello.c-o
Hello.I

At this point, if you look at the contents of the Hello.cpp file, you will find that the contents of the stdio.h are actually plugged into the file, and other macro definitions that should be preprocessed are handled accordingly. The next step is to compile hello.i as the target code, which can be done by using the-C parameter:
# gcc-c Hello.i-o
hello.o

GCC defaults to the. I file as a preprocessed C-language source code, so the above command will automatically skip the preprocessing step and start the compilation process, or you can use the-x parameter to get GCC to start compiling from the specified step. The final step is to link the resulting target file into an executable file:
# gcc Hello.o-o Hello

Both 2.GCC and g++ are compilers of the GNU (an organization).

1, for the. c Suffix of the file, gcc as a C program; g++ as a C + + program;

2. For files with the. cpp suffix, gcc and g++ will be treated as C + + programs.

3. During the compilation phase, g++ will call GCC (i.e.);

4, the connection phase, usually with g++ to complete, this is because the GCC command can not automatically and C + + program to use the library connection.

GCC can be used to compile C + + but it does not automatically invoke the linked C + + library, you need to manually link it, using the following command: Gcc-lstdc++main.cpp. G++ will automatically invoke the linked C + + library.

GCC can be compiled by C programs and C + + programs according to the suffix named. C or. cpp, but g++ is compiled by C + + programs uniformly, either. C or. cpp.
And g++ in the process of compiling, actually call GCC according to C + + program to compile. That is, the compilation work is ultimately done by GCC.
For C + + programs, because GCC commands do not automatically connect to libraries used by C + + programs, you need to manually link C + + libraries, so links can be g++ or gcc-lstdc++. That is to say C + + programs, you can also use GCC to link.

But when GCC compiles the C program, it automatically links to the C library.

Myth One: gcc can only compile C code, g++ can only compile C + + code
Both are possible, but please note:
1. The suffix is. C, GCC treats it as a C program, and g++ as a C + + program; the suffix is. cpp, both of which are considered C + + programs, note that although C + + is a superset of C, there is a difference between the two requirements for syntax. C + + syntax rules are more rigorous.
2. During the compilation phase, g++ will call GCC. For C + + code, it can be compiled with GCC or g++, which is equivalent.
3 link stage, because GCC commands are not automatically joined with the libraries used by C + + programs, so the links are usually done with g++. Generally for convenience, simply compile and link are used g++. This gives people an illusion, as if the CPP program can only be compiled with g++ links.


Myth Two: GCC does not define __cplusplus macros, and g++ will
In fact, this macro simply indicates that the compiler will interpret the code in C or C + + syntax, as described above, if the suffix is. c, and the GCC compiler is used, the macro is undefined, otherwise it is defined.

Myth Three: Compile only with GCC, link only with g++
Strictly speaking, this sentence is not wrong, but it confuses the concept, it should be said: Compile can be used gcc/g++, and links can be used g++ or gcc-lstdc++. Because GCC commands are not automatically joined to libraries that are used by C + + programs, you typically use g++ to complete joins. In the compilation phase, however, g++ automatically calls GCC, which is equivalent.

3.GCC Upgrade Issues

About GCC Installing the default version number with Yum under CentOS 5 is 4.1.2;centos 6 is 4.4.7;centos 7 is 4.8.3.
Most of the time in compiling and installing software requires a high version of GCC, otherwise it will be an error.
So how do I upgrade the GCC version?

The first thing to confirm is the GCC version number that you upgraded to.
The latest GCC version is now 5.2, and CentOS 7 is still using 4.8, so I chose to upgrade to 4.8.5 Based on compatibility considerations.
GCC Official website: https://gcc.gnu.org

Following the steps to compile and install GCC 4.8.5, it is important to note that prior to compiling and installing GCC, the system must first install the old version of GCC and the dependent libraries via Yum.
If it is compiled under the x86_64 system, you also need to install libgcc.i686 glibc-devel.i686.

Yum install-y gcc Texinfo-tex flex zip libgcc.i686 glibc-devel.i686

Of course, if it is installed under the CentOS 5 x86_64 system, it is the following command:

Yum install-y gcc Texinfo-tex flex zip libgcc.i386 glibc-devel.i386

At the same time, it should be noted that the compilation and installation of GCC memory is not less than 1gb,swap not less than 1GB, hard disk is not less than 10GB, otherwise it is very likely to exit the error.
After compiling and installing, the directory gcc-4.8.5 will be more than 5GB.

1. Download the source code

wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.5/gcc-4.8.5.tar.gz

2, download the dependency package
Compiling and installing GCC requires a dependency on the MPC,MPFR,GMP package. Fortunately, the GCC source code comes with a script that can easily download dependent packages.

Tar zxf gcc-4.8.5.tar.gzcd gcc-4.8.5./contrib/download_prerequisites

In this script you can see that the version number of the dependent package is mpc-0.8.1,mpfr-2.4.2,gmp-4.3.2 in turn.

3. Compile and install

mkdir GCC-BUILD-4.8.5CD gcc-build-4.8.5. /configure--prefix=/usrmake && make install

In order to avoid multiple versions of GCC appearing in the system after installation, the compiled and installed directories are specified as/usr directly, and if –prefix is not specified, it will be installed by default under/usr/local.
GCC 4.8.5 Light Source code is 105MB, so you can foresee the entire compilation process takes a long time (about 2 hours or so).

4. View version number

GCC--VERSIONGCC (gcc) 4.8.5g++--versiong++ (gcc) 4.8.5which Gcc/usr/bin/gccwhich g++/usr/bin/g++

Note: This is a successful installation under CentOS 6.7 x86_64.

4. CentOS view files in several ways

There are many commands to view the file, different commands have different advantages, you can select the command separately for different needs to improve efficiency:
Cat starts displaying content from the first line and outputs all content
The TAC displays the contents in reverse from the last line and outputs all the content
More according to the window size, one page of the actual file content
Less is similar to more, but its advantages can be a page forward, and the ability to search for characters
Head only shows the first few lines
Tail show only the last few lines
NL similar to cat-n, output line number when displayed
The most I use is more and less!

1.cat and TAC
The function of cat is to output the contents of the file continuously on the screen from the first line. However, cat is not commonly used, because when a file is large, the number of rows is too high, and the screen cannot be fully allowed, only a subset of the content is visible.

Cat syntax: Cat [-n] File name
Parameter Description:-N: Output with line number when displayed

TAC Syntax: TAC file name

2.more and less (recommended)
More and less are useful compared to cat and TAC.

The more function is to start the file from the first line, according to the Output window size, the appropriate output file content.
When a page cannot be fully output, you can use "enter" to turn down 3 rows (my environment is 3 rows, other Linux versions may be different), or use the "SPACEBAR" to page down.
To exit the view page, press the "Q" key.
In addition, more can be combined with the pipe symbol "|" (pipe), for example: Ls-al | More

3.head and tail
Head and tail are typically used in cases where only the first few lines or the last few lines of the file need to be read.

The head function is to display the first few lines of the file
The syntax of head:
Head "-N number" file name

4.nl
The function of NL is the same as cat-n, which outputs the entire contents from the first line and displays the line number.

NL's Syntax:
NL file Name

Several minor problems with CentOS compiler HelloWorld

Related Article

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.