1、 生成动态链接库
#ifdef EXPORT_API
#define DLL_API extern”C” __declspec(dllexport)
#else
#define DLL_API extern”C” __declspec(dllimport)
#endif
2、 两个系统的选择
#ifdef _WIN32
#include “Thread_Win32.h”
#else
#include “ThreadBase_Vx.h”
#endif
3、 避免重复编译
#ifndef QYDBFCAE_BUSINTERFACE_H
#define QYDBFCAE_BUSINTERFACE_H
Xxx
#endif
4、 C语言编译
#ifdef __cplusplus
extern “C”
{
#endif
Xx
Xx
Xx
#ifdef __cplusplus
}
#endif
#if 常量表达式
//程序段1
#else
//程序段2
#endif
作用:如果常量表达式的内容为真(非0),则对程序1进行编译;否则对程序2进行编译。因此可以使程序在不同条件下,完成不同的功能。
#ifdef 标识符 [标识符唯一化]
//程序段1
#else
//程序段2
#endif
作用:当标识符已经被定义过,则对程序1进行编译;否则编译程序2。
【ifdef与ifndef常常结合起来使用】
例如:
#ifndef EXPORT_API
#define EXPORT_API
#endif
#ifdef EXPORT_API
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
#ifndef 标识符
//程序段1
#else
//程序段2
#endif
作用:当标识符没有被定义则编译程序1,否则编译程序2;
【条件指示符#ifdef检查预编译常量在前面是否已经被定义。
#ifndef的最主要目的使用是防止头文件的重复包含和编译。】