What you can't learn in class C and C + + those things (i)

Source: Internet
Author: User

First, declare that this is a series of articles. As for the whole series of how many articles, I do not know, do not know how many articles, also do not know how long will update an article. Anyway, there is only one principle, written out of the article can be seen by people will be published. In addition, I do not call you to skip class, but feel that lectures are just ordinary students do, listen to listen to the listening, should not listen to listen to even, the classroom is just the big thousand of the programming circle of sand a grain is also not known, the real cow people never meet that a little bit of knowledge, some things too serious you lose, the world is big, Don't confine your vision, that will be very tiring.

First: The entire system environment is based on the Linux platform, if you are interested you can refer here to learn Linux: How to become a real linuxer on the road, but also to promote the team just built the Linuxcoder community.

The compiler's GCC, g++, no, please install the first self.

compiling the first executable file

The first article, as usual, write the most classic hellow Word program. (Code 1)

Code by Lfly

2014-11-22

#include <iostream>

using namespace Std;

int main (int argc, char **argv)

{

cout << "Hellow world!" << Endl;

return 0;

}

A very simple piece of code, which returns 0 for a successful exit, returns another value that represents some kind of error (see specific value). Save as Hellow.cpp file and then compile run under:

[Email protected]:~/project/c> ls hellow.cpp

[Email protected]:~/project/c> g++ hellow.cpp-o HELLOW.O

[email protected]:~/project/c> ls hellow.cpp hellow.o

[Email protected]:~/project/c>

-O is the specified result file name, which is compiled into the target file hellow.o

If you do not specify the file name with-O, the default is to compile the a.out file.

The. Out file is a compiled link to an executable file, and the. o file is generally compiled with a target file that is not yet linked.

But under Linux is not after the prefix name to distinguish whether it is an executable file, the difference between the standard only one: the file under the corresponding user has no Execute permission (x permission). Well, run it:

[Email protected]:~/project/c>./hellow.o

Hellow world!

That's exactly what I wanted: hellow world!

main function Parameters:

There are two parameters in the main function, the first is the int type argc, which indicates the number of arguments passed into the main function. The second one is a two-dimensional character Pointer argv, which holds each incoming parameter. Note here that argv[0] is the path to the execution of this executable file, and then argv[1] to argv[argc-1] to save the user's incoming parameters (if any). Change the program here: (Code 2)

Code by Lfly

2014-11-22

#include <iostream>

using namespace Std;

int main (int argc, char **argv)

{

cout << "ARGC is:" << argc << Endl;

for (int i=0; i<argc; ++i)

{

cout << Argv[i] << Endl;

}

return 0;

}

In this way, the output quantities are argc and the individual strings in the argv. The build command that remains above is then:

g++ Hellow.cpp-o HELLOW.O

Then add up to two parameters Hellow, world run a bit:

[Email protected]:~/project/c>/hellow.o Hellow World

ARGC Is:3.

/hellow.o

Hellow

World

You can see that the number of parameters is 3 because the default first parameter is the path of execution (here is./hellow.o) The remaining two are the incoming hellow and world

Note: The main function can be written without parameters or (void). The main function was initially without parameters. To understand the origins of the main function, see here: What you don't necessarily know about the main () function http://www.nowamagic.net/librarys/veda/detail/96

Spying on the compilation and linking process

g++ compiling the link file procedure:

Pre-processing, compilation (assembly file), assembly (machine code), link (executable program)

1 preprocessing process

The. I file is generated and this look is executed by the preprocessor CPP program.

CPP is an executable program, the general path is/usr/bin/cpp (may be a link), you can use the Find command to search the specific path. The preprocessor reads the source code and then finds the preprocessing directives (macro definitions, file inclusions, conditional compilations) that begin with #.

    • Macro definition

A macro definition refers to a # define directive, which is expanded by the preprocessing process, such as #define DF 10

Preprocessing replaces the DF Independent combination that appears in the program code with 10, which is a simple macro definition, and the macro definition with parameters is not discussed here.

    • file contains

Refers to the # include directive, where the preprocessor replaces the contents of the header file contained in this # include directive.

    • Conditional compilation

#ifdef (#ifndef) with the #endif directive, these instructions are very important to make the compiled target file is not too large, you think that the # include directive will replace the contents of a header file into the CPP file, if you have the idea of repeating the file (file A contains file B, A is included in file C, and then B is included, so C contains two times B), which is difficult to avoid in complex engineering. So using conditional compilation can optimize your program, and of course it has other important functions that are not discussed here.

In addition to processing the precompiled instructions, the preprocessor will also delete your comments (the machine does not look at your comments and is not understood). Then there are reserved #pragma instructions.

OK, now let's see what our hellow world will look like after preprocessing. For simplicity, use the above code 1 as the source code, using g++-e precompilation (you can, of course, use the CPP command directly)

g++-E Hellow.cpp-o hellow.i

Then take a look at the pre-compilation to get the contents of this hellow.i file:

A few lines of code are precompiled to get a full 17563 lines of files. And the two lines of comment that I started are really gone.

2 compiling into a compilation file

The process is to compile the precompiled code into assembly code, which is executed by the compiler Egcs. Here's a compilation of the hellow.i file we got:

g++-S Hellow.i-o hellow.s

Then take a look at the Hellow.s file:

Are compiled code, learn to remember to take care of the body ah .....

3 Assembly Process

This step will give you the. o Target file. The process is performed by the assembler as, translating the assembly instructions from the HELLOW.S file obtained above into machine code.

g++-C Hellow.s-o HELLOW.O

4 Linking process

This is done by the linker LD, which links multiple. O machine codes into. Out executables (of course, the suffix is not the focus). Note that I have only one. cpp file here, so there is only one. o file, no link is required, this is just an example, but there must be more than one file under normal engineering, and then it will be linked to an executable file to run.

These are examples of Linux platforms where you can use g++ to do the same steps below Windows.   But as a program ape, it is recommended that you use Linux for development, do not ask why, you can find my blog about Linux articles to see. This is the first article to discuss here, the next update to discuss other issues.

Welcome to visit my website: http://www.programfish.com

Linuxcoder Community: http://linuxcoder.org

NOTE: Reprint please specify "Guangzhou Linux enthusiasts + cloud computing Jing Jinming"

What you can't learn in class C and C + + those things (i)

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.