How to define the constant _DEBUG in c++ - 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, i.e. 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.

Related

C++ Debug macro failing compile in release mode

I'm using visual studio (2019) and I have this macro:
#if _DEBUG
#define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#endif
My log function this wraps around is:
template<typename... args>
static void Log(args && ... inputs)
{
std::osyncstream bout(std::cout);
bout << ENGINE_TAG;
([&]() {
bout << inputs;
} (), ...);
bout << "\n";
}
Obviously this works fine when I'm in debug mode, but when I use release mode I get:
Error C3861 'EngineLog': identifier not found
I'm unsure why the compiler takes issue here, I was told it would remove the macro from the code when not defined but it seems not to be doing that.
if _DEBUG is not defined, then EngineLog is an unresolved name. You need to add a definition for the other case.
#if _DEBUG
#define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#else
#define EngineLog(vars, ...) (0 && Log(vars, ##__VA_ARGS__))
#endif
The syntax used here prevents any potential warnings about the variadic arguments being unused while at the same time preventing both the function from being called and its parameters being evaluated.
Looks like you need a #else preprocessor case to define out the macro call with an empty expansion:
#if _DEBUG
#define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#else
#define EngineLog(vars, ...)
#endif

How to check 2 defines are the same?

In MFC application, there is a define for diagnosing the memory leak and so on.
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
In afx.h, there exists DEBUG_NEW definition
void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#define DEBUG_NEW new(THIS_FILE, __LINE__)
How to check new is defined as DEBUG_NEW?
I tried to use
#if defined(new) && new == DEBUG_NEW
It does not work.
If you have access to the MFC application code in question, I would change the #ifdef contruct to:
#ifdef _DEBUG
#define new DEBUG_NEW
#define NEW_REDEFINED
#endif
Then you can do your check using #ifdef NEW_REDEFINED
The idea of redefining a keyword in a macro is a somewhat crazy one and I certainly hope nobody else follows this MFC idea. In that case, a simple
#ifdef new
would suffice. If the default new is used, the keyword won't be defined as a macro.
Depending on what specifically you're after, another option might be to simply use
#ifdef _DEBUG
trusting that new is defined in the form you expect if and only if _DEBUG is defined.
But C++ does not have any check that can be performed in an #if expression that will tell you if a macro is defined in a particular form.
If you only need to know if the two definitions are the same at runtime you could use something like:
#include "string.h"
#define STR(s) #s
#define compare(a, b) (strcmp(STR(a), STR(b)) == 0)
This would compare the exact string used to define each macro, so it may only be useful in certain cases, consider:
#define MY_INT1 int
#define MY_INT2 int
#define MY_INT3 (int)
#define MY_FLOAT float
compare(MY_INT1, MY_INT2) // Evaluates to true
compare(MY_INT1, MY_INT3) // Evaluates to false
compare(MY_INT1, MY_FLOAT) // Evaluates to false

What is the difference between VERIFY and ATLVERIFY

What is the difference between the C++ macro VERIFY() and ATLVERIFY() ? And which one is better to use for COM objects?
There is some difference in how the errors are reported. ATLVERIFY is defined as
#ifndef ATLVERIFY
#ifdef _DEBUG
#define ATLVERIFY(expr) ATLASSERT(expr)
#else
#define ATLVERIFY(expr) (expr)
#endif // DEBUG
#endif // ATLVERIFY
And ATLASSERT is
#ifndef ATLASSERT
#define ATLASSERT(expr) _ASSERTE(expr)
#endif // ATLASSERT
So it ends up in _ASSERTE (see https://msdn.microsoft.com/en-us/library/ezb1wyez.aspx )
While VERIFY is
#ifdef _DEBUG
#define VERIFY(f) ASSERT(f)
#else // _DEBUG
#define VERIFY(f) ((void)(f))
#endif // !_DEBUG
So it ends up in ASSERT (see https://msdn.microsoft.com/en-us/library/aa297139(v=vs.60).aspx )

Conditional preprocessing puzzle

I have a problem where I can't seem to get conditional #define preprocessors to work correctly. For example:
#define WIN32_BUILD
#ifdef WIN32_BUILD
#define PCH "stdafx.h"
#else
#define PCH "xyz.h"
#endif
#include PCH
If I use this form, the compiler tells me that it can't find 'stdafx.h'. OK, that seems odd, so if I change the code to....
#define WIN32_BUILD
#ifdef WIN32_BUILD
#define PCH "xyz.h"
#else
#define PCH "stdafx.h"
#endif
#include PCH
Then the file defined in PCH gets picked up and everything compiles fine. This seems odd to me, almost like the preprocessor is ignoring the #if directives and just using all the #defines that it encounters.
Obviously I am doing something wrong, and I was hoping that someone could help me understand this.
When a project has the precompiled header feature turned on the preprocessor ignores everything that comes before #include "stdafx.h"
So your #define statements are ignored.
TL:DR; #define defines the symbol, #ifdef tests if the symbol is defined not whether it has a value.
#define WIN32_BUILD
This defines a pre-processor token, WIN32_BUILD. The token has no value. Anywhere you use the token 'WIN32_BUILD' the pre-processor will substitute the empty string, i.e. nothing.
#ifdef WIN32_BUILD
This checks if the pre-processor token WIN32_BUILD is defined. It is, you just defined it.
#ifdef WIN32_BUILD
// true - this code is included.
#define PCH "stdafx.h"
This defines the pre-processor token, PCH, and assigns it the value "stdafx.h"
#else
#define PCH "xyz.h"
#endif
This code is ignored, because WIN32_BUILD was defined.
It looks as though you were expecting 'ifdef' to only evaluate to true if the expression was not defined /to/ something.
#define a
#define b SOMETHING
#ifdef a
// you are expecting this to be ignored
#endif
#ifdef b
// and expecting this not to be ignored
#endif
#ifdef and #if defined(...) do the same thing.
#define a
#define b SOMETHING
#if defined(a) && defined(b)
// this code will be evaluated, both tokens are defined.
#endif
This feature of pre-processor tokens is often used to support conditional functionality:
#if HAVE_CPP11_OVERRIDE_KEYWORD
#define OVERRIDE_FN override
#else
#define OVERRIDE_FN
#endif
struct A {
virtual void foo() {}
};
struct B : public A {
void foo() OVERRIDE_FN {}
};
In the above code, the override keyword is only added if the system supports it (determined outside of the code).
So a compiler with override sees
struct B : public A {
void foo() override {}
};
a compiler without it sees
struct B : public A {
void foo() {}
};
Note: The opposite of "ifdef" is "ifndef":
#define a
#define b SOMETHING
#undef c
//#define d // << we didn't define it.
int main() {
#ifdef a
#pramga message("a is defined")
#else
#pramga message("a is UNdefined")
#endif
#ifdef b
#pragma message("b is defined")
#else
#pramga message("b is UNdefined")
#endif
#ifdef c
#pramga message("c is defined")
#endif
#else
#pramga message("c is UNdefined")
#endif
#ifdef d
#pramga message("d is defined")
#endif
#else
#pramga message("d is UNdefined")
#endif
#ifndef d
#pragma message("d is not defined")
#endif
#ifndef a
#pragma message("a is not defined")
#endif
return 0;
}
You can assign a pre-processor token numeric values and test them with #if
#if _MSC_VER
#define WIN32_BUILD 1
#else
#define WIN32_BUILD 0
#endif
#if WIN32_BUILD
#include <Windows.h>
#endif
But, especially when doing cross-platform programming, people tend to use ifdef variants rather than numeric checks, because the value checks require you to explicitly ensure all of the tokens are defined with a value. It's a lot easier just to only define them when you need them.

Is there a neat method to output text to debugging pane?

I would like to display some log messages when debugging. One option is to use the very ugly
#ifdef DEBUG
std::cout << "I'm in debug mode!\n";
#endif
In the JUCE library, there is a nice macro that outputs text to the debugging pane
DBG("I'm in debug mode!")
The juce solution also allows you to do neat stuff like the following that would be desirable
int x = 4;
DBG(String("x=") + String(x))
I would like to know if a similarly neat method exists in std:: or boost::
Why not just write your own:
#ifdef DEBUG
#define DBG(x) std::cout << x;
#else
#define DBG(x)
#endif
For namespaces
namespace DBG
{
inline void DBG(const char* x)
{
#ifdef DEBUG
std::cout << x;
#endif
}
}
If you want something like printf, you should use a bit another macros:
void DebugPrintLn(const char* format, ...);
inline void Nothing(...) {}
#ifdef DEBUG
#define DBG DebugPrintLn
#else
#define DBG Nothing // Or __noop on Visual C++
#endif
Using Nothing is portable, but arguments still computed (__noop guarantees that any of argument will be not computed, VC++ specific). Better if you can use macros variable arguments (both available on GCC and latest VC++): you may even skip any argument computation in portable way:
#ifdef DEBUG
#define DBG(...) DebugPrintLn(__VAR_ARGS__)
#else
#define DBG(...) ((void)0)
#endif
In any case, you use it the same way:
DBG("Lucky day: %s %i", "Friday", 13);
I have also written my own portable TRACE macro. I share it here:
#ifdef ENABLE_TRACE
# ifdef _MSC_VER
# include <windows.h>
# include <sstream>
# define TRACE(x) \
do { std::stringstream s; s << (x); \
OutputDebugString(s.str().c_str()); \
} while(0)
# else
# include <iostream>
# define TRACE(x) std::clog << (x)
# endif // or std::cerr << (x) << std::flush
#else
# define TRACE(x)
#endif
example:
#define ENABLE_TRACE //can depend on _DEBUG or NDEBUG macros
#include "my_above_trace_header.h"
int main (void)
{
int v1 = 123;
double v2 = 456.789;
TRACE ("main() v1="<< v1 <<" v2="<< v2 <<'\n');
}
Any improvements/suggestions/contributions are welcome ;-)