[C/C++] DebugBreak

时间:2024-04-18 10:12:56

在代码中直接调用DebugBreak()函数,可以使程序中断运行,和在IDE中设置断点中断运行的道理是一样的。

用这种方式,一些情况下比打断点更方便调试,如下,在test()函数返回0时激活断点

#include "debugapi.h"

int test(int x)
{
if (x > 0) return 1;
return 0;
} int _tmain(int argc, _TCHAR* argv[])
{
if (test(-1) == 0)
{
DebugBreak();
} return 0;
}

有时编译时编译器会报一个"No Target Architecture"错误,解决的方法是在项目属性中加入"_x86_"或"_IA64_"预编译宏。