Remove function in Release build only
Is there any methods to remove a line of code from a Release build, but leave it in the Debug build without ugly #if
statements?
For example, is the开发者_JAVA百科re some way to achieve the equivalent of the below code without using all these if statements?
#if DEBUG
Log.Log("I am in debug mode");
#endif
If I have a conditional, run-time check in the Log.Log
function, then the string "I am in debug mode" will be preserved within my compiled executable, which is exactly what I do not want.
Define another macro in a common, shared header.
#ifdef DEBUG
#define LOG(m) Log.Log(m);
#else
#define LOG(m) do {} while(false);
#endif
Then replace your calls to Log.Log
with LOG
.
You'll ultimately need a preprocessor conditional somewhere, but you could apply it "upstream" in some shared header if you want to keep your application code clean. In that case, you'd have something like
#if DEBUG
#define DebugLog(m) Log.Log(m);
#else
#define DebugLog(m)
#endif
in the header associated with Log
, and instead of calling Log.Log(m) inside a preprocessor conditional in your application code, you'd just call DebugLog(m)
. In a Debug build, the macro would expand to Log.Log(m), but otherwise it would just disappear entirely.
Have your Log.Log
function #ifdef'd based on the build. So in DEBUG, it logs stuff, and in RELEASE, it's a no-op. For example:
namespace Log
{
void Log()
{
#if DEBUG
//Insert logging code here.
#endif
}
}
精彩评论