我正在从事一个多平台项目。我想要一种简单快捷的方法来管理特定于操作系统的包含,最好不需要任何外部工具。
我想要这样的东西(我很清楚这是行不通的)
#define PLATFORM_A 1
#define PLATFORM_B 2
#ifndef TARGET_PLATFORM
//ERROR
#endif
#if TARGET_PLATFORM == PLATFORM_A
#define DIR "a"
#elif TARGET_PLATFORM == PLATFORM_B
#define DIR "b"
#define PLATFORM_INCLUDE(file) \
#include "platforms/" DIR "/" file
最佳答案
我经常看到的一个设置如下:
// platform.h
#if defined PLATFORM_A
#include "platform_a.h"
#elif defined PLATFORM_B
#include "platform_b.h"
#else
// #pragma error or whatever
#endif
然后在必要时#include "platform.h"
。
关于C++ 平台特定包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551042/