条件编译的行为类似于C语言中的if...else...
,条件编译是预编译指示命令,用于控制是否编译某段代码。
条件编译初探
通过下面这个例子,我们初探条件编译时如何工作的。
// #include <stdio.h>
#define C 1
int main()
{
const char* s;
#if( C == 1 )
s = "This is first printf...\n";
#else
s = "This is second printf...\n";
#endif
// printf("%s", s);
return 0;
}
预编译gcc -E lmcjl.com.c -o lmcjl.com.i
结果:
拿掉注释,编译运行结果:
条件编译的本质:
if...else...
语句在运行期进行分支判断,条件编译指令在预编译期进行分支判断gcc -Dmacro=value file.c
或gcc -Dmacro file.c
#include <stdio.h>
int main()
{
const char* s;
#ifdef C
s = "This is first printf...\n";
#else
s = "This is second printf...\n";
#endif
printf("%s", s);
return 0;
}
执行如下编译命令:
gcc -DC lmcjl.com.c
运行结果:
条件编译的本质:
#include
的间接包含同样会产生嵌入文件内容操作间接包含同一头文件是否会产生编译错误?
// global.h
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
int global = 10;
#endif
// test.h
#ifndef _TEST_H_
#define _TEST_H_
#include "global.h"
const char* NAME = "test.h";
char* hello_world(){
return "Hello lmcjl.com!\n";
}
#endif
lmcjl.com.c
#include <stdio.h>
#include "test.h"
#include "global.h"
int main()
{
const char* s = hello_world();
int g = global;
printf("%s\n", NAME);
printf("%d\n", g);
return 0;
}
运行结果:
条件编译可以解决头文件重复包含的编译错误:
#ifndef _HEADER_FILE_H_
#define _HEADER_FILE_H_
//source code
#endif
#if...#else...#endif
被预编译器处理,而if...else...
语句被编译器处理,必然被编译进目标代码product.h
#define DEBUG 1
#define HIGH 1
lmcjl.com.c
#include <stdio.h>
#include "product.h"
#if DEBUG
#define LOG(s) printf("[%s:%d] %s\n", __FILE__, __LINE__, s)
#else
#define LOG(s) NULL
#endif
#if 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");
#if 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;
}
运行结果:
本文链接:http://so.lmcjl.com/news/23857/