C++条件编译 DEBUG

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

#ifdef DEBUG
   cerr <<"Variable x = " << x << endl;
#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

猜你喜欢

转载自blog.csdn.net/aa804738534/article/details/112917618
今日推荐