c++ - 定义宏错误 : expected primary-exception before 'asm'

标签 c++ debugging assembly macros breakpoints

这段代码应该提供一个断言函数,仅用于调试,如果断言失败它会自动创建一个断点(不是完整代码):

#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/

相关文章:

c++ - 使用多个描述符集与更多统一更新

java - SML 中的 printf 调试

c++ - 如何找出我的 .exe 的入口函数?

linux - gcc,静态库,外部汇编函数变成 undefined symbol

c++ - 管道优化,这样做有什么意义吗?

c++ - 这是一个有效的 Copy Ctor 吗?

c++ - 为子窗口指定窗口过程

c++ - avx2 8 浮点寄存器上的水平最小值并随机排列配对寄存器

delphi - 在delphi 6中查找无限循环

cocoa - Mac OS X 的汇编 GUI 编程