C and C ++ tasks that cannot be learned in the classroom (1). Those in the classroom

Source: Internet
Author: User

C and C ++ tasks that cannot be learned in the classroom (1). Those in the classroom

First, declare that this is a series of articles. As for the number of articles in the series, I do not know, I do not know how many articles there are, and I do not know how long it will take to update the article. There is only one principle, and the written documents can be published. In addition, I don't mean you to skip the course, but think that the lectures are just for average students. When you listen to the lectures, you just need to listen, the things in the class are nothing more than a grain of sand in the programming world. The real cool people never satisfy that little bit of knowledge. Some things are too serious and you lose, and the world is huge, do not limit your own vision. It will be tiring.

First, the entire system environment is based on the linux platform. If you are interested, you can refer to here to learn about linux: how to become a real er on the road, we also promoted the newly established LinuxCoder community.

Gcc and g ++ used by the compiler. If not, install them on your own.

 

Compile the first Executable File

In the first article, write the most classic hellow word program as an example. (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. The final return of 0 indicates that the exit is successful, and the return of other values indicates an error (see the specific value ). Save as the hellow. cpp file and compile and run it:

Lfly @ programfish :~ /Project/c> ls hellow. cpp

Lfly @ programfish :~ /Project/c> g ++ hellow. cpp-o hellow. o

Lfly @ programfish :~ /Project/c> ls hellow. cpp hellow. o

Lfly @ programfish :~ /Project/c>

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

If you do not use-o to specify the file name, the file is compiled into a. out by default.

. Out files are compiled and linked to executable files, while. o files are usually compiled into a target file without links.

However, in Linux, the extension name is not used to determine whether the file is an executable file. There is only one criterion for the difference: whether the file has the execution permission (x permission) under the corresponding user ). Run the following command:

Lfly @ programfish :~ /Project/c>./hellow. o

Hellow World!

It's exactly what I want: Hellow World!

 

Main function parameters:

The main function has two parameters. The first one is the int type argc, which indicates the number of parameters passed into the main function. The second is a two-dimensional character pointer argv, which stores the input parameters. Note that argv [0] stores the path for executing the executable file, the following argv [1] to argv [argc-1] is the parameter that stores user input (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, each string in the argc and argv is output. Still the above compilation command and then:

G ++ hellow. cpp-o hellow. o

Add two parameters hellow and world to run the job:

Lfly @ programfish :~ /Project/c>./hellow. o hellow world

Argc is: 3.

/Hellow. o

Hellow

World

We can see that the number of parameters is 3, because the first parameter by default is the execution path (here it is./hellow. o) and the other two are the imported hellow and world

Note: The main function can be written without parameters or void. The main function initially did not contain parameters. To understand the life of the main function Please see here: you do not necessarily understand the main () function thing http://www.nowamagic.net/librarys/veda/detail/96

 

View compilation and link Process

G ++:

Preprocessing-> compilation (Assembly file)-> assembly (machine code)-> Link (executable program)

1. Preprocessing Process

Generate the. I file, which is executed by the pre-processor cpp program.

Cpp is an executable program. The general path is/usr/bin/cpp (which may be a link). You can use the find command to search for a specific path. The pre-processor reads the source code and finds the pre-processing commands (macro definition, file inclusion, and Conditional compilation). These commands start.

  • Macro definition

Macro definition refers to the # define command, which will be expanded in the preprocessing process, for example, # define DF 10

Preprocessing replaces the independent DF combinations in the program code with 10. This is a simple macro definition. macro definitions with parameters are not discussed here.

  • File Inclusion

Indicates the # include command. The Preprocessor will replace the content of the contained header file with the # include command.

  • Conditional compilation

# Ifdef (# ifndef) and # endif commands. These commands are used to prevent compilation of object files from being too large, the # include command above will replace the content of A header file with the cpp file. If you repeatedly include the file (File A contains file B, if file C contains A and B, then C contains B twice. This is difficult to avoid in complex projects. So Conditional compilation can optimize your program. Of course, it has other important functions, which will not be discussed here.

In addition to processing pre-compiled commands, the pre-processor also deletes your comments (the machine does not read your comments, but it cannot be understood ). Then the # pragma command is retained.

Now let's take a look at what we will look like after hellow world preprocessing. For simplicity, use code 1 as the source code and use g ++-E for pre-compilation (of course, you can directly use the cpp command)

G ++-E hellow. cpp-o hellow. I

Then let's take a look at the pre-compiled content of the hellow. I file:

A few bytes of code can be pre-compiled to get 17563 lines of files. The two lines at the beginning of the comment do not exist.

2. compile it into an assembly File

In this process, the pre-compiled code is compiled into assembly code and executed by the compiler egcs. Compile the hellow. I file as follows:

G ++-S hellow. I-o hellow. s

Check the hellow. s file:

It's all assembly code. Remember to take care of your health when learning assembly .....

3 Assembly Process

In this step, we can get the. o target file. The process is executed by the assembler as, and the Assembly commands in the hellow. s file obtained above are translated into machine codes one by one.

G ++-C hellow. s-o hellow. o

4. Link Process

It is completed by the linker ld, and multiple. o machine codes are linked to executable files such as. out (of course, suffix names are not important ). Note: I only have one. cpp file, so there is only one. the o file does not need to be linked. This is just an example. But in a normal project, there must be more than one file. At that time, it must be linked to an executable file before it can be run.

The above is an example in Linux. You can use g ++ in windows to perform the same steps. But as a programmer, we recommend that you use linux for development. Don't ask why. Please refer to the linux article on my blog. The first article will be discussed here. We will discuss other issues in the next update.

 

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

LinuxCoder community: http://linuxcoder.org

Note: Please note that "Author: Linux fans in Guangzhou + Yun Jinming for cloud computing"

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.