简体   繁体   中英

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
}
#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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM