这段代码应该提供一个断言函数,仅用于调试,如果断言失败它会自动创建一个断点(不是完整代码):
#define DebugInt3 asm __volatile__ (".byte 0x90CC")
#define DEBUG_ASSERT(expr) ((expr)? ((void)0): (DebugInt3))
但是,我得到了这些错误:
error: expected primary-expression before 'asm'
error: expected ')' before 'asm'
error: expected ')' before ';' token
令我感到困惑的是,如果我直接将它放在主函数中,“asm volatile (".byte 0x90CC")”会起作用。 Stackoverflow 删除了双下划线,顺便让单词变粗。我做错了什么?
解决方案和示例,感谢 Richard Pennington:
#include <iostream>
#include <cassert>
#ifdef DEBUG
// 0xCC - int 3 - breakpoint
// 0x90 - nop?
#define DebugInt3 asm __volatile__ (".byte 0x90CC")
#define DEBUG_ASSERT(expr) do { if (!(expr)) DebugInt3; } while(false)
#endif
using namespace std;
int main()
{
#ifdef DEBUG
DEBUG_ASSERT(false);
// assert(false);
#endif // DEBUG
return 0;
}
最佳答案
您正在尝试将语句用作表达式。尝试类似的东西
#define DEBUG_ASSERT(expr) do { if (!(expr)) DebugInt3; } while(0)
关于c++ - 定义宏错误 : expected primary-exception before 'asm' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27712346/