Jack Robson
Jack Robson

Reputation: 2302

How to define the constant _DEBUG in c++

When I compile and execute thes code, I get...

_DEBUG IS NOT defined

Why isn't the constant being shown as defined?

using namespace std;

int main() {
  const bool _DEBUG = true;
  #if defined _DEBUG
    std::cout << "_DEBUG IS defined\n";
  #else
    std::cout << "_DEBUG IS NOT defined\n";
  #endif // _DEBUG
}

Upvotes: 0

Views: 469

Answers (3)

aep
aep

Reputation: 1675

const bool _DEBUG = true; defines a constant which is known to the compiler and not to the preprocessor.

The following check is executed by the preprocessor before the compiler kicks in, therefore it never sees _DEBUG constant.

  #if defined _DEBUG
    std::cout << "_DEBUG IS defined\n";
  #else
    std::cout << "_DEBUG IS NOT defined\n";
  #endif // _DEBUG

To get rid of the issue, you should #define _DEBUG so that the preprocessor knows about the token.

Upvotes: 1

Nate Eldredge
Nate Eldredge

Reputation: 58493

#if defined TOKEN only checks if TOKEN is defined as a preprocessor macro, i.e. with #define TOKEN .... Here you have defined it as a (constant) variable, which is not the same thing.

Upvotes: 1

i486
i486

Reputation: 6573

#define _DEBUG

or

#define _DEBUG       1

The second method can be checked with #ifdef _DEBUG or #if _DEBUG. Usually _DEBUG is defined in compiler IDE profile.

Upvotes: 3

Related Questions