I have a DEBUG_PRINT macro defined in the header file for a shared object that I wrote that looks like this:
lib_mylib.h:
#ifdef DEBUG
#define DEBUG_TEST 1
#else
#define DEBUG_TEST 0
#endif
#define DEBUG_PRINT(fmt, args...) \
do { if (DEBUG_TEST) fprintf(stderr, "%s:%d:%s(): " fmt, \
__FILE__, __LINE__, __FUNCTION__, ##args); } while (0)
From a c file that uses this library, I have set
#DEFINE DEBUG 1
which results in the DEBUG_PRINT statement being used successfully where it is called within the shared object. However, when I use the statement in my c file, it compiles but doesn’t get executed – why is this?
The preprocessor is strictly sequential, macros have to be defined before they are used or checked for.