我正在编写一个C程序,它使用自定义日志记录功能来调试我的程序.每当我将程序编译为发布版本时,我都希望从代码中删除所有日志记录功能,以便在有人试图反汇编时它不会显示.
请看以下示例:
#include <stdio.h>
void custom_logging_function(char* message)
{
// Do something here
}
int main()
{
custom_logging_function("Hello world"); // This call should be removed.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做,以便它custom_logging_function和它的参数不会被编译到我的程序中而不必在我的代码中到处编写包含警卫?谢谢
您可以使用预处理器标志,例如:
#include <stdio.h>
#ifdef DEBUG
void custom_logging_function(char* message)
{
// Do something here
}
#else
#define custom_logging_function(x) ((void) 0)
#endif
int main()
{
custom_logging_function("Hello world"); // This call should be removed.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,您必须告诉定义"调试"目标DEBUG,如果您想为"发布"目标定义一些可以替换#ifdef DEBUG的内容#ifndef NDEBUG,并将NDEBUG标志添加到"发布"定义中.
编辑:
改变#define custom_logging_function(x) 0以#define custom_logging_function(x) ((void) 0)通过@JoachimPileborg他的回答启发.