Search code examples
c++arduinoesp32arduino-esp32

Macro which will not compile the function if not defined


Currently using to show debug output when in debug mode:

#ifdef _DEBUG
#define printX(...) Serial.printf( __VA_ARGS__ )
#else
#define printX(...) NULL
#endif 

yet this still include the printX in the result code, and the parameters which have been applied still consume memory, cpu power, and stack size, so my question is:

  • is there a way to have a macro, which is not including the function, and "ignoring" all of it's calls in the source when in "release mode" and basically not compile anything with it ?

Solution

  • The macro

    #define printX(...) NULL
    

    replaces printX function call with all its arguments with plain NULL. This is a textual replacement that happens before a compiler is able to take a look at the code, so any nested calls inside printX, e.g.

    printX(someExpensiveCall())
    

    will also be completely eliminated.