C/C ++ explains the process from the source code to the executable program, and explains the source code.

Source: Internet
Author: User

C/C ++ explains the process from the source code to the executable program, and explains the source code.

The C/C ++ source code generates executable files on the platform. Three steps are required: preprocessing, compilation, and linking.

1. Preprocessing

Execute commands such as macros, Conditional compilation commands, and header file inclusion. That is to say, this is a work of code replacement.

After the C/C ++ source code is preprocessed, various macro commands no longer exist. However, after expansion, the code is still in the form of readable.

2. Compile

Translate C/C ++ code into assembly code, and then translate it into the machine code of the platform. A target file with the same name as the cpp file will be generated, and its suffix is. o or. obj. The target file exists in binary format.

Generally, a target file has two segments:

① Code segment

② Data Segment

This step has the largest workload and is also the most time-consuming step.

3. Links

Link the target file generated in step 3 with various required library files to generate the platform executable file.

In addition to linking the target file to the library file, the linker is also responsible for the link of unresolved symbols. Unresolved symbols are symbols modified by keywords such as extern.

There are two types of links:

① Internal link: If a name (variable or function) is partial for its compilation unit (cpp), and the link does not conflict with the same name of other compilation units, the name has an internal link.

② External link: If a name (variable or function) can interact with other compilation units (cpp) during the link, the name has an external link.

As shown in the preceding figure. the H file will be compiled by multiple units (I .e. cpp) contains, if. h. the compilation unit of the H file defines the same symbol. If the symbol is an external link, the definition is visible to all compilation units, this will cause the duplicatedexternal symbols Link error (note that this is a link error ). Therefore, if the header file is to be defined, you must ensure that the defined symbols are only internal links.

C ++ features:

For C ++, a compilation unit refers to A. cpp file and all. H files it includes. During preprocessing, the code in. h will replace the # include command to expand it to. cpp.

Then the compiler compiles the. cpp file and generates a. obj file, which is the binary target file.

After all .cppis are compiled by the compiler, the linker links the target file and the library file to generate an. EXE file or library file.

Visual Studio:

Visual Studio is a development tool set. For C/C ++ programs, it integrates compilers and connectors.

1. Read the configuration in Visual Studio and determine the standard function library and header file. Include paths in environment variables.

2. Visual Studio determines the dependency between source codes.

The so-called dependency refers to that if file B References File A, you must first compile file A and then compile file B. Therefore, based on the dependency, the compiler must meet the following requirements:

① B can be started only after A is compiled.

② When A changes, B will be re-compiled

In fact, the compilation sequence is saved in the makefile file generated by the configure script. This is why configure must be run before compilation.

We can see that, while determining the compilation sequence, the header files used for compilation are also determined.

3. Compile the pre-compiled header file in Visual Studio

4. Prepare Visual Studio and replace various macro commands.

5. compile Visual Studio, convert the source code to the assembly code, and then convert it to the target code (machine code)

6. Connect to Visual Studio. If it is a static link, copy the function library to the executable file directory. If it is a dynamic link, it is called only at runtime.

Note that operations such as static link copy function libraries must be manually performed, and Visual Studio cannot automatically perform these operations.

This is why we need to manually copy the library file to the generated executable directory. In addition, operations such as program cleanup will not affect these library files in the executable directory.

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.