动态库
参考 Qt
的写法,我们很容易得到以下写法:
#ifndef DECL_EXPORT_AND_DECL_IMPORT
#define DECL_EXPORT_AND_DECL_IMPORT
#ifdef _WIN32
#define DECL_EXPORT __declspec(dllexport)
#define DECL_IMPORT __declspec(dllimport)
#else
#define DECL_EXPORT
#define DECL_IMPORT
#endif
#endif
#if defined(__DLL_LIBRARY)
#define RESULTSDISPLAY_EXPORT DECL_EXPORT
#else
#define RESULTSDISPLAY_EXPORT DECL_IMPORT
#endif
class RESULTSDISPLAY_EXPORT TestClass{
//...
}
条件编译常用宏
跨平台编译常用的宏有:
- WIN32
该宏定义在minwindef.h
中,这意味着如果你需要使用它,那么你得包含Windows API
头文件:
// Win32 defines _WIN32 automatically,
// but Macintosh doesn't, so if we are using
// Win32 Functions, we must do it here
#ifndef WIN32
#define WIN32
#endif
- _WIN32 & _WIN64
_WIN32
定义的初衷是为了区分16
位和32
位程序,但是目前很多项目的代码都是使用_WIN32
来判定是否是windows
平台,所以目前在windows
平台下编译32
位和64
位程序时,编译器都会预定义了_WIN32
。
一般32位编译器只定义 _WIN32
,64位程序会定义 _WIN32
及 _WIN64
。
如果你仅需在 Windows
及 Linux
平台做兼容,则仅需判断是否为Windows环境即可:
#ifdef _WIN32
// Windows
#else
// Other
#endif