是否可以用字符串检查预处理器定义?

Is it possible to check preprocessor defines with string?

我想知道是否可以使用字符串检查预处理器定义的变量是否存在。

例如,

#define TARGET_ANDROID

if (checkIfDefineExists("TARGET_ANDROID"))
{
    cout << "It is defined\n";
}

那么结果应该打印 It is defined.

据我所知这是不可能的,但我想知道是否有任何解决方法。

As far as I know it is not possible

正确。

but I wonder if there's any work around to it.

没有。但是如果你的目标是找出你的程序是针对什么目标编译的,你可以这样做:

#if defined(TARGET_ANDROID)
#  define TARGET "ANDROID"
#elif defined(TARGET_IOS)
#  define TARGET "IOS"
#endif

if (checkIfTargetIs("ANDROID")) {
    cout << "It is ANDROID\n";
}

问可不可以也没意义,因为"it"还没有定义

#define TARGET_ANDROID
if 
#undef TARGET_ANDROID
 (
#define TARGET_ANDROID
  checkIfDefineExists
#undef TARGET_ANDROID
   (
#define TARGET_ANDROID
     "TARGET_ANDROID"
#undef TARGET_ANDROID
    )
#define TARGET_ANDROID
  )
#undef TARGET_ANDROID
{ 
cout << "It is defined\n";
}

您希望您的程序打印什么,为什么?