您可以只在调试时进行编译,调试开关可以使用一个宏来实现,如下所示:

#ifdef DEBUG
cerr <<"Variable x = " << x << endl;
#endif

#define DEBUG //debug调试
#ifdef DEBUG
#define DEBUG_LINE() printf("[%s:%s] line=%d\r\n",__FILE__, __func__, __LINE__)
#endif

 

#include <iostream>
using namespace std;
#define DEBUG

#define MIN(a,b) (((a)<(b)) ? a : b)

int main()
{
int i;
int j;
i=100;
j=30;
#ifdef DEBUG
cerr <<"Trace:Inside main function"<<endl;
#endif


#if 0
/* 这是注释部分 */
cout << MKSTR(HELLO C++) << endl;
#endif

cout <<"The minimum is " << MIN(i, j) << endl;

#ifdef DEBUG
cerr <<"Trace: Coming out of main function" << endl;
#endif
return 0;
}

运行结果:
Trace:Inside main function
The minimum is 30
Trace: Coming out of main function

 

#include <iostream>
using namespace std;
//#define DEBUG //取消

#define MIN(a,b) (((a)<(b)) ? a : b)

int main()
{
int i;
int j;
i=100;
j=30;
#ifdef DEBUG
cerr <<"Trace:Inside main function"<<endl;
#endif


#if 0
/* 这是注释部分 */
cout << MKSTR(HELLO C++) << endl;
#endif

cout <<"The minimum is " << MIN(i, j) << endl;

#ifdef DEBUG
cerr <<"Trace: Coming out of main function" << endl;
#endif
return 0;
}

运行结果:
The minimum is 30