当前位置:网站首页>【C】 Compilation preprocessing and environment

【C】 Compilation preprocessing and environment

2022-06-11 18:00:00 Penguins don't cry

Catalog

Preface

One 、 Translation environment and execution environment

Two 、 compile + link

        1. precompile

         2. compile

        3. assembly

         4. Running environment

3、 ... and 、 Preprocessing

        1. Defining symbols

        2.#define

        3.# and ## 

        4. The comparison between macro and function  

        5.#undef

        6. Conditional compilation

summary


Preface


One 、 Translation environment and execution environment

The source file to the executable program must pass through the translation environment , The translation environment includes compilation and linking , Compile package precompile 、 compile 、 assembly . The source file is processed by the compiler to generate the target file (.obj), Object files and link libraries generate executable programs through linkers .

Two 、 compile + link

        1. precompile

        The essence is text operation :

        1. The header file contains #include

        2. Delete Note

        3.#define Defines the substitution of symbols

         2. compile

         hold C Language into assembly code

        1. Syntax analysis

        2. Lexical analysis

        3. Semantic analysis

        4. Symbol summary

        3. assembly

         Convert assembly code into binary instructions :

         4. Running environment

1. The program must be loaded into memory

2. Start execution , call main function

3. The program uses the run stack , Storage function local variable return address

4. To terminate the program

3、 ... and 、 Preprocessing

        1. Defining symbols

__FILE__
__LINE__
__DATE__
__TIME__
__STDC__
// Source files to compile
// The current line number of the file
// The date the file was compiled
// When the file was compiled
// If the compiler follows ANSI C, Its value is 1, Otherwise, it is not defined

        2.#define

#define name( parament-list ) stuff
// Among them  parament-list  It's a list of symbols separated by commas , They may appear in stuff in 
#define SQUARE( x ) x * x
// This macro takes a parameter  x .
// If after the above statement , You put 
SQUARE( 5 );
// Put in the program , The preprocessor will replace the above expression with the following expression 
5 * 5

Tips :

         So the macro definitions used to evaluate numeric expressions should be bracketed in this way , Avoid using macros due to . Unexpected interactions between operators or adjacent operators .
 

        3.# and ## 

int i = 10;
#define PRINT(FORMAT, VALUE)
    printf("the value of " #VALUE "is "FORMAT "\n", VALUE);

// Output results 
the value of i+3 is 13
//## You can combine the symbols on both sides of it into one symbol .
// It allows macro definitions to create identifiers from detached pieces of text .

#define ADD_TO_SUM(num, value) 
    sum##num += value;
ADD_TO_SUM(5, 10);// Role is : to sum5 increase 10.

        4. The comparison between macro and function  

Naming conventions :

         Capitalize all macro names
         Do not capitalize all function names
 

        5.#undef

#undef NAME
// If an existing name needs to be redefined , Then its old name must first be removed 

        6. Conditional compilation

#ifndef __TEST_H__
#define __TEST_H__
// Content of header file 
#endif //__TEST_H__

  perhaps

#pragma once

          This is done to avoid duplicate definitions of header files . When the header file is included for the first time _TEST_H Because there's no definition , So satisfy ifndef_TEST_H, Define after executing downward _TEST_H. The next time the header file is included, it has already been defined _TEST_H, So I'm not satisfied ifndef_TEST_H. They don't execute #ifndef_TEST_H To #endif Part between .


summary

        see 《 High-quality C/C++ Programming Guide 》 The test paper in the appendix .

原网站

版权声明
本文为[Penguins don't cry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011854587561.html