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

标签 c debugging release armcc rvds

我使用 LOG_DEBUG 函数将调试信息打印到屏幕上。我使用 #define _DEBUG 通过在编译时(发布时)定义 _DEBUG FLAG 来禁用 LOG_DEBUG 函数。但是发布构建应用程序的 linux strings 命令仍然显示已编译应用程序中存在的调试字符串。那么有什么方法可以消除 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

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

优化:-O3

最佳答案

您可以尝试使用 stringification :

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

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

关于c - 在发布版本中删除调试字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44317362/

相关文章:

c - 如何在C中将两个一维数组相乘后生成一个二维数组?

c - 为什么我的TTL值每次都增加2? (C Socket编程)

python - 为什么我的 Vimspector 调试 session 没有初始化?

c++ - 发布 C++ 宏定义

spring-boot - Heroku Docker Spring 启动镜像错误503 H14

c - 让编译器声明并定位调试指针变量

ruby - Rails 3.0 间歇性连接超时、执行过期错误

debugging - GDB 中断点停止时自动查看高级指令?

Maven - 发布 :prepare without committing

c - BST 插入操作。如果已经存在重复节点,则不要插入节点