編譯預處理
專題三:編譯預處理。包括以下章節:
- 編譯過程簡介
- 宏定義與宏使用分析
- 條件編譯使用分析
- #error和#line
- #pragma預處理分析
- #和##運算符使用解析
基本概念
- 條件編譯的行爲類似於c語言中的if…else…語句
- 條件編譯是預編譯指示命令,用於控制編譯哪一段代碼
條件編譯對預編譯器起作用;if…else…在運行時起作用
#if()…#else…#endif 判斷條件表達式
- #ifdef…#else…#endif 判斷宏
- #ifndef…#endif 判斷是否定義過宏:如果定義過,跳出;否則,往下進行
3-1.c
#include<stdio.h>
//如果沒有定義宏常量C,可以通過命令:gcc -DC=1 -E 3-1.c -o 3-1.i 來定義宏常量C,方便調試。
#define C 1
int main()
{
#if( C == 1)
printf("THIS IS FIRST ...\n");
#else
printf("THIIS IS SECOND ...\n");
#endif
if (c == 1) {
printf("THIS IS FIRST ...\n");
}else {
printf("THIIS IS SECOND ...\n");
}
return 0;
}
3-1.i
# 1 "3-1.c"
# 1 "<built-in>"
# 1 "<命令行>"
# 1 "3-1.c"
/*......此處省略n行......*/
# 940 "/usr/include/stdio.h" 3 4
# 2 "3-1.c" 2
int main()
{
printf("THIS IS FIRST ...\n");
if (1 == 1) {
printf("THIS IS FIRST ...\n");
}else {
printf("THIIS IS SECOND ...\n");
}
return 0;
}
#include
- #include是將已經存在的文件內容拷貝到當前文件中
- 間接包含同一個文件也會拷貝相同的內容到當前文件中,會出現編譯錯誤
3-2.c
#include "3-2.h"
#include "global.h"
int main()
{
return 0;
}
3-2.h
#include "global.h"
global.c
int global = 2;
3-2.i
# 1 "3-2.c"
# 1 "<built-in>"
# 1 "<命令行>"
# 1 "3-2.c"
# 1 "3-2.h" 1
# 1 "global.h" 1
int global = 2; //重定義了global
# 2 "3-2.h" 2
# 2 "3-2.c" 2
# 1 "global.h" 1
int global = 2;
# 3 "3-2.c" 2
int main()
{
return 0;
}
結果:
- 我們可以使用條件預編譯指令(#ifndef:判斷是否定義過宏; #define:定義宏定義; #endif:結束)解決嵌套包含問題
3-2.c
#include "3-2.h"
#include "3-2.h"
#include "global.h"
#include "global.h"
int main()
{
return 0;
}
3-2.h
#ifndef _3_2_H_
#define _3_2_H_
#include "global.h"
#endif
global.c
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
int global = 2;
#endif
3-2.i
# 1 "3-2.c"
# 1 "<built-in>"
# 1 "<命令行>"
# 1 "3-2.c"
# 1 "3-2.h" 1
# 1 "global.h" 1
int global = 2;
# 5 "3-2.h" 2
# 2 "3-2.c" 2
int main()
{
return 0;
}
結果編譯正確:
條件編譯的意義
- 條件編譯使得我們可以按不同的條件編譯不同的代碼段, 因而可以產生不同的目標代碼
- #if…#else…#endif被預編譯器處理;而if…else語句被 編譯器處理,必然被編譯進目標代碼
- 實際工程中條件編譯主要用於一下兩種情況:
- 不同的產品線共用一份代碼
- 區分編譯產品的調試版和發佈版
實例分析3-2:條件編譯的使用
3-3.c
#include <stdio.h>
#ifdef DEBUG
#define LOG(s) printf("[%s:%d] %s\n", __FILE__, __LINE__, s)
#else
#define LOG(s) NULL
#endif
#ifdef HIGH
void f()
{
printf("This is the high level product!\n");
}
#else
void f()
{
}
#endif
int main()
{
LOG("Enter main() ...");
f();
printf("1. Query Information.\n");
printf("2. Record Information.\n");
printf("3. Delete Information.\n");
#ifdef HIGH
printf("4. High Level Query.\n");
printf("5. Mannul Service.\n");
printf("6. Exit.\n");
#else
printf("4. Exit.\n");
#endif
LOG("Exit main() ...");
return 0;
}
結果:
小結
- 通過編譯器命令行能夠定義預處理器使用的宏
- 條件編譯可以避免重複包含頭同一個頭文件
- 條件編譯是在工程開發中可以區別不同產品線的代碼
- 條件編譯可以定義產品的發佈版和調試版