#ifdef _DEBUG
a = "ram";
#else
a = "sam";
#endif
It is working fine in Visual Studio as _DEBUG is defined in pre-processor, whereas it is not working in Linux even though I gave "-g" option while compiling. What would be the alternative for this? I want the same piece of code for both platforms.
The standard macro for this is NDEBUG, though it has the opposite sense of _DEBUG (the N is for "not"). So you can write your code portably like this:
#ifndef NDEBUG
a = "ram";
#else
a = "sam";
#endif
Note how I used #ifndef to negate the conditional. Of course you could write it equivalently as:
#ifdef NDEBUG
a = "sam";
#else
a = "ram";
#endif
If you don't like this, you can also define _DEBUG on Linux by compiling like this:
g++ -g -D_DEBUG ...
Related
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
I'm in a situation where I have code that looks like this :
#ifdef NOISE
#ifdef NOISE_2D
DECLARE_NOISE2D();
#else // NOISE_3D
DECLARE_NOISE3D();
#endif
#else
DECLARE_NO_NOISE();
#endif
Is there a way to have Clang-format look at this piece of code and format it as such :
#ifdef NOISE
#ifdef NOISE_2D
DECLARE_NOISE2D();
#else // NOISE_3D
DECLARE_NOISE3D();
#endif
#else
DECLARE_NO_NOISE();
#endif
The best I've been able to do is with IndentPPDirectives: BeforeHash, which gives this as a result :
#ifdef NOISE
#ifdef NOISE_2D
DECLARE_NOISE2D();
#else // NOISE_3D
DECLARE_NOISE3D();
#endif
#else
DECLARE_NO_NOISE();
#endif
I also tried to pass preprocessor directives as macro block starts and ends, but it seems like they don't get picked up (worth a try).
It looks like preprocessor directives and C++ code have their own indentation tree that they don't seem to share. Is there anyway to have Clang-format give the desired output, or alternatively, is there any other automatic formatter that would be able to provide this functionality ?
I used the answer from Determining 32 vs 64 bit in C++ to make this:
#ifndef AVUNA_CFG
#define AVUNA_CFG
#if _WIN32 || _WIN64
#if _WIN64
#define BIT64
#else
#define BIT32
#endif
#endif
// Check GCC
#if __GNUC__
#if __x86_64__ || __ppc64__
#define BIT64
#else
#define BIT32
#endif
#endif
#endif
However, this doesn't seem to work when specifying -m32 to GCC for cross compiling, so it always says BIT64. Is there any defines I can use for this purpose?
I ended up using an Eclipse-define because I have two different run configurations for 32/64-bit cross compile. Works well.
I can do this in Visual C++ 2008 with Release (NDEBUG) setting:
debug.h
#ifdef _DEBUG
void debug_printf(const char* format, ...);
#else
#define debug_printf(format, v) __noop
#endif
debug.cpp
#include "stdafx.h" //#include "debug.h" is inside it
void debug_printf(const char* format, ...) {
//so much work here
}
but not anymore in Visual C++ 2013, I will get compile error in debug.cpp file. It seems I have to change the defining strategy in debug.h. But I wonder is there compiler setting to reenable again the old way?
Use a macro in the first case too, and let it call the actual function (which is named something different from the macro).
And in the second case, just have an empty macro body.
Use variadic macros.
Something like
#ifdef _DEBUG
# define debug_printf(fmt, ...) real_debug_printf(fmt, __VA_ARGS__)
#else
# define debug_printf(fmt, ...)
#endif
When _DEBUG is not defined, then the macro debug_printf is replaced by nothing (or rather, an empty line).
For example if I had this code:
#ifdef _DEBUG
mPluginsCfg = "plugins_d.cfg";
#else
mPluginsCfg = "plugins.cfg";
#endif
Can I define a macro that looks like
#define DEBUG_RELEASE(debug_code, release_code)
and then use it like this;
DEBUG_RELEASE(mPluginsCfg = "plugins_d.cfg";,mPluginsCfg = "plugins.cfg";)
I'm sure that it works, and I'm almost sure that it is defined to work.
#ifdef _DEBUG
#define DEBUG_RELEASE(d,r) d
#else
#define DEBUG_RELEASE(d,r) r
#endif
I'm unsure whether I've seen anything uglier in the wonderful world of preprocessor macros.