在发布版本中删除调试字符串

Man*_*evs 6 c debugging release rvds armcc

我使用一个LOG_DEBUG函数将调试信息打印到屏幕上.我使用a #define _DEBUGLOG_DEBUG通过_DEBUG在编译时(发布时间)定义FLAG 来禁用函数.但strings发布版本应用程序的linux 命令仍然显示编译应用程序中存在的调试字符串.那么消除争论的替代方法是LOG_DEBUG什么?

#ifdef _DEBUG
#define LOG_DEBUG(fmt, ...) printf("[D][%s:%d %s]", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#else
#define LOG_DEBUG(fmt, ...)
#endif


LOG_DEBUG("this is a debug string");     // "this is a debug string" exists in app release build yet
Run Code Online (Sandbox Code Playgroud)

我使用的编译器: ARM/Thumb C/C++ Compiler, RVCT3.1 [Build 569]

优化: -O3

squ*_*age 3

您可以尝试使用字符串化

#include <stdio.h>

#define _DEBUG

#ifdef _DEBUG
#define LOG_DEBUG(str) do { printf("[D][%s:%d %s] ", \
                               __FILE__, \
                               __LINE__, \
                               __FUNCTION__); \
                            puts(#str); } while(0)
#else
#define LOG_DEBUG(str)
#endif

int main() {
  LOG_DEBUG(this is a debug string);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

注意:我在 clang 中对此进行了测试,它没有表现出您所描述的行为。