前言:
有的时候我们需要多种配置,在敲代码的时候我们设定好各种配置,然后根据我们的需要配置一个开关,在源代码级别去修改配置开关让程序编译出不同的效果。
常见的条件编译
1、#if #else #elif #endif
2、#ifdef #endif
如下例子就是通过是否定义宏DEBUG来控制控制台是否打印
#include <stdio.h>#define DEBUG#ifdef DEBUG#define debug(x) printf(x)#else#define debug(x)#endifint main(void){ debug("this is a debug info.\r\n"); return 0;}#ifndef #define 和#endif 这种类型的条件编译一般用在头文件中用来防止头文件重复包含.
#ifndef __DEMO_H__#define __DEMO_H__int my_add(int x,int y);#endif