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

We have a C ++ library. We provide user affirmations and disclaimers from Posix NDEBUGand assert(story below).

The statement looks like this on Windows:

#  define CRYPTOPP_ASSERT(exp) {                                  \
    if (!(exp)) {                                                 \
      std::ostringstream oss;                                     \
      oss << "Assertion failed: " << (char*)(__FILE__) << "("     \
          << (int)(__LINE__) << "): " << (char*)(__FUNCTION__)    \
          << std::endl;                                           \
      std::cerr << oss.str();                                     \
      DebugBreak();                                               \
    }                                                             \
  }

The task that we have is that we must include <windows.h>, and this leads to a lot of extra steepness, even with WIN32_LEAN_AND_MEAN. Some additional cracks, such as minand max, break compilation in C ++. In fact, testing our changes has broken us.

<windows.h> " ", . extern void WINAPI DebugBreak(void); Microsoft docs DebugBreak, - .

NO_MIN_MAX ( , ) , , . , . #include .

#pragma push_macro #pragma pop_macro , Microsoft V++ 6.0. VS2003.

- <windows.h>. DebugBreak Windows.h?

.


:

// cl.exe /c assert.cpp

#include <algorithm>

// #include <windows.h>
// #ifndef WINAPI
// # define WINAPI __stdcall
// #endif
// extern void WINAPI DebugBreak(void);

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

void dummy(int x1, int x2)
{
    MY_ASSERT(x1 == std::min(x1, x2));
}

extern. Windows 8.1 x64 VS2012 VS2013. .

Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

assert.cpp
assert.cpp(11) : warning C4273: 'DebugBreak' : inconsistent dll linkage
        C:\Program Files (x86)\Windows Kits\8.1\include\um\debugapi.h(70) : see
previous definition of 'DebugBreak'
assert.cpp(21) : error C2589: '(' : illegal token on right side of '::'
assert.cpp(21) : error C2059: syntax error : '::'
assert.cpp(21) : error C2143: syntax error : missing ';' before '{'

<debugapi.h>, :

WINBASEAPI
VOID
WINAPI
DebugBreak(
    VOID
    );

WINBASEAPI . , .


- ++, CVE-2016-7420. - , . , ( ); (Apple CrashReporter, Ubuntu Apport, Microsoft Windows Error Reporting, ..).

/ , Makefile Visual Studio . / , ++ throw() . /, .

, , , " / -DNDEBUG" . ; RTFM , . , CMake , Autotools , Eclipse .. , CVE. , , , .

+4
3

, :

__debugbreak();
+3

, :

#include <windows.h>

void MyDebugBreak(void)
{
   DebugBreak();
}

MyDebugBreak() DebugBreak() .

Windows, #if .

+2

DebugBreak , Visual Studio 6 2015, __stdcall extern "C". V++ 6, , std::min , , , 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;
}
+1

Source: https://habr.com/ru/post/1654934/


All Articles