Rashed Azad Rashed Azad - 2 months ago 15
C++ Question

Confusion with DEBUG macro

test.cpp

#include <iostream>
#include "Class1.h"
#define DEBUG

int main() {
checkAssert();
}


Class1.h

#include <cassert>

#ifndef CLASS1_H_
#define CLASS1_H_

#if defined(DEBUG)

void checkAssert(){
int number = 10;
assert(number == 10);
}


#else

void checkAssert(){
std::cout << "opps" << std::endl;
}

#endif /* DEBUG */
#endif /* CLASS1_H_ */


1. I have defined DEBUG in main file.

2.In Class1.h #if defined(DEBUG) is for check if DEBUG is defined or not(according to my understanding).

I am trying this program to understand how DEBUG macro works in c++, But every time I have opps output in the screen.
Can anyone please help me to understand what is going on.

Answer

Your test.cpp sets the macro after the header file is included already. That's too late. You have to set the macro before including the header file:

#define DEBUG
#include <Class1.h>