-1

I wonder if it is possible to check if the preprocessor defined variable exists or not using string.

For example,

#define TARGET_ANDROID

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

Then the result should print It is defined.

As far as I know it is not possible, but I wonder if there's any work around to it.

Zack Lee
  • 2,784
  • 6
  • 35
  • 77
  • 3
    Are you looking for `#ifdef TARGET_ANDROID` or `#if defiined(TARGET_ANDROID)`? The preprocessor _changes the program text_ before the front end even sees the code. – 1201ProgramAlarm May 24 '19 at 04:42
  • @1201ProgramAlarm No I'm not looking for a way to check it by comparing with a string. – Zack Lee May 24 '19 at 04:45
  • 1
    How are you getting the string? Is it available during compile time like your example code or is it only available during run time? – taskinoor May 24 '19 at 04:46
  • 1
    @ZackLee `#define` are consumed by the preprocessor. This means that those defines are done when the program is ready to be compiled. What you can do is to use `#ifdef` to define variables or strings to be read in your program – Amadeus May 24 '19 at 04:49
  • @taskinoor It is only available during run time. – Zack Lee May 24 '19 at 05:09
  • 1
    @ZackLee then you can't check it. If you describe what you are trying to achieve really then someone may be able to provide an alternate approach. – taskinoor May 24 '19 at 05:11
  • 1
    No, this is not possible as described. Can you give more details about your high level goal? – Brennan Vincent May 24 '19 at 05:15
  • That's exactly for what the preprocessor command `#ifdef`is for. – Chris623 May 24 '19 at 05:18

2 Answers2

2

As far as I know it is not possible

Correct.

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

No. But if your goal is to find out for what target your program was compiled, you can do:

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

if (checkIfTargetIs("ANDROID")) {
    cout << "It is ANDROID\n";
}
Nikos C.
  • 50,738
  • 9
  • 71
  • 96
1

It doesn't even make sense to ask whether it is possible, because "it" has not been defined.

#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";
}

What do you want your program to print, and why?

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • It's complicated to explain but I'm trying to convert #defines to Lua global variables. It is related to this question: https://stackoverflow.com/questions/56268080/best-way-to-convert-defines-into-lua-global-variables – Zack Lee May 24 '19 at 07:35
  • You are trying to convert `#define`s that are in effect *where exactly*? – n. m. could be an AI May 24 '19 at 07:38