条件编译
上代码
// what is the fucking shit.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#define VERSION_XZM 1
#define VERSION_HAERBIN _T("哈尔滨\n")
#define VERSION_TIANJIN _T("天津\n")
#define VERSION_SHAGNHAI _T("上海\n")
//
//
#define VERSION_01
#define VERSION_02
#define VERSION_03
#define VERSION_04
#define VERSION_05
// undef
//
#undef VERSION_01
#undef VERSION_02
#undef VERSION_03
#undef VERSION_04
#undef VERSION_05
int _tmain(int argc, _TCHAR* argv[])
{
int year = 2018 ;
#if VERSION_XZM == 1
OutputDebugString(_T("已定义--版本1\n"));
#elif VERSION_XZM == 2
OutputDebugString(_T("已定义--版本2\n"));
#elif VERSION_XZM == 3
OutputDebugString(_T("已定义--版本3\n"));
#elif VERSION_XZM == 4
OutputDebugString(_T("已定义--版本4\n"));
#elif VERSION_XZM == 5
OutputDebugString(_T("已定义--版本5\n"));
#endif
#ifdef VERSION_HAERBIN // #if defined VERSION_HAERBIN
OutputDebugString(VERSION_HAERBIN);
#elif defined VERSION_TIANJIN
OutputDebugString(VERSION_TIANJIN);
#elif defined VERSION_SHAGNHAI
OutputDebugString(VERSION_SHAGNHAI);
#endif
#ifndef VERSION_HAERBIN
OutputDebugString(VERSION_HAERBIN);
#elif defined VERSION_TIANJIN
OutputDebugString(VERSION_TIANJIN);
#elif defined VERSION_SHAGNHAI
OutputDebugString(VERSION_SHAGNHAI);
#endif
#if defined VERSION_01
OutputDebugString(_T("已定义--版本01\n"));
#elif defined VERSION_02
OutputDebugString(_T("已定义--版本02\n"));
#elif (!defined VERSION_03) || (!defined VERSION_04)
OutputDebugString(_T("未定义--版本03和版本04\n"));
#elif !defined VERSION_05
OutputDebugString(_T("未定义--版本05\n"));
#endif
return 0;
}
上图
1.#ifdefVERSION_HAERBIN 和 #if definedVERSION_HAERBIN的功能是一样的
2.#ifdef VERSION_HAERBIN如果定义了VERSION_HAERBIN宏,则
3.#ifVERSION_XZM == 1 如果VERSION_XZM 的值等于1则执行
4.#elif defined VERSION_TIANJIN否则,如果定义了VERSION_TIANJIN宏,则
5.#elif (!definedVERSION_03) || (!definedVERSION_04)否则,如果即没定义VERSION_03也没定义VERSION_04,则
6.#endif结束条件编译
7.如果条件刚开始成立了,就直接结束了
#if
#elif
#elif
#endif
8.忘记了#else了,哈哈
接下来应该记录一些常用的宏定义才是,下篇再说