How to get a declaration for DebugBreak without including Windows.h?

感情迁移 提交于 2019-12-05 16:44:31

You can use the intrinsic, it works without includes:

__debugbreak();

Just add a new source code file which contains nothing but:

#include <windows.h>

void MyDebugBreak(void)
{
   DebugBreak();
}

Export as necessary, and call MyDebugBreak() instead of DebugBreak() in your macro.

You can either include the file only in Windows builds, or add #if blocks as appropriate.

Declaring DebugBreak seems to work ok using Visual Studio 6 and 2015 provided you declare it with __stdcall and extern "C". As VC++ 6 doesn't seem to include std::min in the algorithm header I've modified your example a bit but if the first argument is greater than the second it raises the assertion when built using cl -nologo -W3 -O2 -Zi -NDEBUG -o assert.exe assert.cpp -link -subsystem:console -debug

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

extern "C" extern void __stdcall DebugBreak(void );

#define MY_ASSERT(exp) { \
   if (!(exp)) {         \
     DebugBreak();       \
   }                     \
}

void dummy(int x1, int x2)
{
    MY_ASSERT(x1 < x2);
}

int main(int argc, char *argv[])
{
    if (argc != 3) {
        fprintf(stderr, "usage: assert integer integer\n");
        exit(1);
    }
    int a = strtol(argv[1], NULL, 0);
    int b = strtol(argv[2], NULL, 0);
    dummy(a, b);
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!