The life course of a c&c++ program

Source: Internet
Author: User

Turned a lot of blog, content points, did not find what I want, now absorb the essence of the great God, plus my humble opinion, summarized as follows:

A C or C + + program from the beginning you write, to the end, the whole process, what has been done, see below:

First look at the general process: look at the picture:


The main thing I want to say here is what the program has done at each stage, and this is sometimes important, for instance:

#define BSC//#define BMC/* #define EMC *///1. Double slash note BSC lining is a goog boy//2./**/note BMC lining is good boy EMC
The top of the macro replacement, it is easy to understand, but this is really wrong, the reason is very simple, the comment precedes the preprocessing instruction processing, and the macro substitution is in the preprocessing time processing, when the macro takes the substitution, the annotation has been processed, then the question comes, how do we know, all kinds of processing has been done, The question goes back to the beginning, now to analyze:

--"pretreatment
Including:

#define #error#include#if#else#elif#endif#ifdef#ifndef#undef#line#pragma

All preprocessing commands begin with the symbol #.

A #define

The command # define defines an identifier and a string. Each time the identifier is encountered in the source program, it is substituted with the defined string. The ANSI standard defines an identifier as a macro name and replaces the replacement process with a macro substitution. The general form of the command is:

#define Identifier string

Analysis:

1 The statement has no semicolon. There can be any space between the identifier and the string, and once the string starts, it ends with a new line.

2 macro names can be defined as part of other macro name definitions.

3 macro substitution simply replaces the macro identifier with a string of text, provided that the macro identifier must be identified independently or not replaced. For example:

#define XYZ This is a TES

Use macro printf ("xyz");//The segment does not print "This is a test" and prints "xyz". Because the precompiled compiler recognizes "XYZ"

4 If the string is longer than one row, you can continue the line at the end of the line with a backslash ' \ '.

#defineLONG_STRING "This was a very longstring that's used as an example"

5 C language programs generally use uppercase letters to define identifiers.

6 One of the great benefits of replacing a real function with a macro substitution is that the macro substitution increases the speed of the code because there is no overhead for the function call. But increasing the speed also has a price: The program length is increased due to repeated coding.

Two #error

Command #error forcing the compiler to stop compiling for program debugging

#error指令使预处理器发出一条错误消息, the message contains the text in the instruction. The purpose of this instruction is to give certain information before the program crashes.

Three #include

The command #i nclude causes the compiler to embed another source file with an # include source file, and the source file that is being read in must be enclosed in double or angle brackets. For example:

# include "Stdio.h" or # include

Both lines of code use the C compiler to read in and compile the subroutine used to process the disk file library.

It is possible to embed a file within a file in the #i nclude command, which is called a nested embedded file, and the nesting hierarchy is dependent on the specific implementation.

If the explicit path name is part of the file identifier, only those subdirectories are searched for the embedded file. Otherwise, if the file name is enclosed in double quotation marks, the current working directory is retrieved first. If no files are found, at the command line, clear the

Search in all directories. If the file is still not found, search for the standard directory defined when the implementation is implemented.

If you do not have an explicit pathname and the file name is surrounded by angle brackets, it is first retrieved from the directory in the compilation command line. If the file is not found, the standard directory is retrieved and the current working directory is not retrieved.

Four conditional compilation commands

There are several commands that can be selectively compiled for each part of the program's source code, which is called conditional compilation. Commercial software companies extensively apply conditional compilation to provide and maintain many customer versions of a program

#if, #else, #elif及 #endif

If the constant expression after #if的一般含义是如果 # if is true, the code between it and #endif is compiled, otherwise the code is skipped. The command #endif identifies the end of an # if block.

#if constant-expressionstatement sequence#endifeg: #define Max 91#includeusing namespace Std;int main () {#if max > 99< c0/>cout<< "Max is bigger than" < #elif max >       cout<< "Max is bigger than" < #else       cout< ;< "MAX is smaller than" < #endif       return 0;}

An expression that follows # if is evaluated at compile time, so it must contain only constants and identifiers that are already defined, and cannot use variables. The expression is not allowed to contain the operator sizeof (sizeof is also a compile-time evaluation).

#else命令的功能有点象C语言中的else; #else建立另一选择 (in the case of # if failure). Note that the #else属于 # if block.

#elif命令意义与ELSE if the same, it forms an if else-if ladder-like statement, which can be used for many compilation choices. #elif followed by a constant expression. If the expression is true, the subsequent code block is compiled, not the other #elif expression

To test. Otherwise, test the next piece sequentially.

#if expressionstatement sequence#elif expression1statement sequence#endif<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > </span>
In nested conditional compilation, #endif, #else或 #elif match the most recent # if or #elif.

# ifdef and # ifndef
Another way to conditionally compile is to use the #ifdef and #ifndef commands, which represent "if there is a definition" and "if there is no definition" respectively. # The general form of ifdef is:
# ifdef Macronamestatement sequence#endif
#ifdef与 #ifndef can be used for # if, #else, #elif语句中, but must be associated with a #endif.
#define MAX 91#includeusing namespace std; int main () {#ifdef MAX       cout<< "hello,max!" < #else       cout<< "where is MAX?" < #endif #ifndef Leo       cout<< "Leo is not defined" < #endif       return 0;}
Command #undef cancels the definition of a macro name that was previously defined. The general form is:
#undef macroname
The command #line changes the contents of __line__ and __file__, which are pre-defined identifiers in the compilation program. The basic form of the command is as follows:
#line number["filename"]
Where the number is any positive integer, the optional file name is any valid file identifier. The line number is the current line number in the source program, and the file name is the name of the source file. Command #line is mainly used for debugging and other special applications. Note: Behind the #line

A digital ID that starts on the next line.

#line "Jia"       cout<< "#line change line and filename!" <       cout<<__line__<       cout<<__file__<
Five #pragma

The command #pragma is an implementation-defined command that allows a variety of instructions to be sent to the compiler.

#pragma的作用是设定编译器的状态或者是指示编译器完成一些特定的动作. #pragma指令对每个编译器给出了一个方法, the host or operation is given when it remains fully compatible with the C and C + + languages

System-specific features. By definition, the compilation instructions are proprietary to the machine or operating system and are different for each compiler.

The format is generally:

#Pragma Para

1 message parameter.

The message parameter can output the corresponding information in the compilation Information Output window, which is very important for the control of source code information. It is used in the following ways:

#pragma message ("text")

When the compiler encounters this instruction, it prints the message text in the compilation Output window.

When we define a number of macros in the program to control the source code version, it is possible for us to forget that we have the correct settings for these macros, and we can use this instruction to check at compile time. Suppose we want to convict

Have you broken yourself in the source code where the definition of _X86 this macro can be used in the following ways

#ifdef _X86#PRAGMA Message ("_x86 macro activated!") #endif当我们定义了_X86这个宏以后, the application will display "_x86 macro activated!" in the Compile Output window at compile time.
2 code_seg parameters.

Formats such as:

#pragma code_seg (["Section-name" [, "Section-class"]]

It is able to set the code snippet where the function code is stored in the program, which is used when we develop the driver.


3 #pragma once (more commonly used)
As long as this command is added at the very beginning of the header file, the header file is guaranteed to be compiled once. This instruction is actually available in VC6, but it is not used much because of compatibility.

4 #pragma hdrstop
This means that the precompiled header file ends, and the subsequent header files are not precompiled. BCB can precompile the header file to speed up the link, but if all the header files are precompiled and may be too much disk space, use this option

Exclude some header files

Sometimes there are dependencies between units, such as cell A depends on unit B, so unit B is compiled before unit A. You can use #pragma startup to specify the compilation priority, and if you use the #pragma package (smart_init), BCB will

Compiled according to the priority size.

5 #pragma resource "*.DFM"

Indicates that the resources in the *.DFM file are added to the project. The *.DFM includes the definition of the form's appearance.

6 #pragma warning (disable:4507; once:4385; error:164)

Equivalent to:

#pragma warning (disable:4507 34)/* Does not display the 4507 and 34th warning messages. You can use this instruction if there is always a No. 4507 warning and 34th warning at compile time and  there is no doubt that there will be no error. */#pragma warning (once:4385)//No. 4385 warning information is reported only once #pragma warning (error:164)//164th warning message as an error. This pragma warning also supports the following formats: #pragma warning (push [, N]) #pragma warning (pop) here N represents a warning level (1---4). #pragma warning (push) to save an existing warning state for all warning messages. #pragma warning (push, N) holds the existing warning state for all warning messages and sets the global warning level to N. #pragma warning (pop) pops the last warning message to the stack and cancels all changes made between the stack and the stack. For example: #pragma warning (push) #pragma warning (disable:4705) #pragma warning (disable:4706) #pragma warning (disable:470 7)//... #pragma warning (pop)
At the end of this code, all warning messages (including 4705,4706 and 4707) are re-saved.

7 pragma comment (...)

This directive places a note record into an object file or an executable file

The commonly used LIB keyword can help us to connect to a library file.

8 Progma Pack (n)

Specifies how the structure is aligned. #pragma pack (n) to set the variable to n-byte alignment.

N-byte alignment means that the offset of the starting address at which the variable is stored is in two cases:

First, if n is greater than or equal to the number of bytes consumed by the variable, the offset must satisfy the default alignment

Second, if n is less than the number of bytes occupied by the type of the variable, then the offset is a multiple of n and does not satisfy the default alignment.

The total size of the structure also has a constraint, in the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the number of spaces occupied by the variable occupying the largest space, otherwise it must be a multiple of n.


Disclaimer: Partial reprint, hope is useful to you;

——— "Compiler:

The precompiled output file will only have constants, such as numbers, strings, variable definitions, and C-language keywords such as main, if, else, for, while, {,}, +,-, *, \, and so on.

What the precompiled program does is to translate it into an equivalent intermediate code representation or assembly code after confirming that all instructions conform to the grammar rules by lexical analysis and parsing.

——— "Compilation

The assembler process actually refers to the process of translating assembly language code into a target machine instruction. For each C language source process that is processed by the translation system, it will eventually get the corresponding target file through this processing.

A machine language code that is stored in the target file, which is the target equivalent to the source program.

——— "Linking programs

The target files generated by the assembler are not immediately executed, and there may be many unresolved issues.

For example, a function in one source file might refer to a symbol defined in another source file (such as a variable or function call), a function in a library file might be called in a program, and so on.

All of these problems need to be resolved by the process of the linked program. The main task of the linker is to connect the relevant target files to each other, and the symbols to be referenced in one file are the same as the symbols in another file.

Define the connection.

If there is a later discovery, it will continue;

Enlighten


The life course of a c&c++ program

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.