C + + Generate two-level file process (pre-processing, compile-to-link)

Source: Internet
Author: User

Reprint please specify the source of Windows C + + programming, through the VC generation project, write C + + source files, point running, code no problem directly out of the results. VC everything to help us, do not understand the process is absolutely no problem. Go to Linux to write C + +, always feel a bit empty, after all, many times need to build themselves. Online to find a number of related articles, mostly speak advanced, understand its process later write a not advanced but easy to understand, convenient memories. Where there is inaccuracy, please correct me.  c++ includes the source file (. cpp) and the header file (. h), the header file contains the declaration of the variable and the class definition, and the source file contains the definition of the variable. Of course you can also use the source file to organize the program, but using the. h file can make the program structure clearer. Under Linux, C + + generation binaries typically use 1 commands to g++ source1.cpp source2.cpp Source3.cpp-o Main This process consists of preprocessing, compiling, and linking 3 processes, preprocessing containing macro definitions, containing directives, and conditional compilation directives. The   macro definition is a simple substitution, replacing one variable value with another, and then using it as a global variable (#define PI 3.14), which can contain other C + + files before learning C. Typically contains. h files, which can be expanded after preprocessing (the files contained in the. h file are also expanded). Conditional compilation controls the expansion of code by using macro definitions and include directives. For example: main.cpp include "Student.h", include "Teacher.h", student.h file also include "Teacher.h", Then Teacher.h is include 2 times, teacher class repeat definition, compile will error. In Teacher.h, you can use conditional compilation (#ifndef _teacher_h #define _TEACHER_H ... #endif) to ensure that TEACHER is defined only once. The   compilation process translates the code for each compilation unit into machine code, and each compilation unit produces a target file containing unresolved symbol tables, export symbol tables, address redirection tables, and machine codes. Variables, functions are divided into outer links (extern, symbol export) and inner links (static, symbols are not exported) static and extern static: variable x, defined globally in the Example.h file, can only be used within this compilation unit, without exposing the variable (the link does not bring the other compilation unit to the variable). Therefore, the A.cpp file (a compilation unit) can use the variable x in this compilation unit as long as the include "example.h". B.cpp (The B compilation unit) also include "example.h", or the variable x can be used within its compilation unit.  Using the respective version of X, links do not conflict with each other. extern: Declaration of global variables, variables can be declared multiple times, without error. extern int x, this compilation unit will manipulate a variable x, but the definition of x does not compile the unit. So compile-time found that access not defined by the variable x does not error, the link will find its definition, multiple compilation units share a global variable. Therefore, the definition of the global variable in the. h file is either static, and each compilation unit is used in its own. Either the extern declaration, I'm going to use a global variable, and if the definition is not found, find it in the other compilation unit. If you define a variable in the. h file, it is unreasonable that the. h file may be a multiple compilation unit include, which is bound to occur when the variable is redefined. The        linking process links multiple target files (depending on the 3 tables of the target file) to form a complete binary program. There are possible errors at this point: the extern variable is not found, and a variable is defined more than once.   above is the C + + source file to the executable process, details than this description is much more complex, but still have to have bone and meat. Welcome to discuss together.

C + + generates two-level file procedures (pre-processing, compile-to-build links)

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.