tags:

views:

243

answers:

1

How can I prevent the debug popup window from appearing when an assertion fails on a Windows machine? The app I'm writing is console based and I'm using assert() to test certain things when it's executed in test mode. I'm using MinGW + GCC 4.

Edit: This is the test program.

#include <stdlib.h>
#include <assert.h>

int main(void) {
    _set_error_mode(_OUT_TO_STDERR);
    assert(0 == 1);
    return EXIT_SUCCESS;
}

Flags: gcc -mwindows -pedantic -Wall -Wextra -c -g -Werror -MMD -MP -MF ...

Tried without -mwindows as well. I still get the debug popup no matter what. This is on a Vista x86 machine.

+6  A: 

There are many ways you can do that. The crudest is to redefine the assert macro (see the mingw assert.h header). You can also call (which is what I would advise):

_set_error_mode (_OUT_TO_STDERR);

Edit: Really, it works for me:

#include <stdlib.h>
#include <assert.h>

int main (void)
{
  _set_error_mode (_OUT_TO_STDERR);
  assert (0 == 1);
  return 0;
}

Compile with gcc -mwindows, it doesn't show the dialog box at runtime. Remove the line with _set_error_mode, and it shows the dialog box. If it doesn't work for you, please give a complete example.

FX
Don't you mean `_OUT_TO_STDERR` ?
MSalters
Yes I did. Corrected, thanks.
FX
This is odd. When I call the function in `main()` I get implicit function declaration and undefined macro errors. But I do have stdlib.h included and I checked the declarations myself - they're there. The macro is defined as `# define _OUT_TO_STDERR 1` and the function as `_CRTIMP int __cdecl __MINGW_NOTHROW _set_error_mode (int);`. Any idea why I am getting the errors?
Ree
If you compile with `-ansi`, then you'll get this. Otherwise, no idea; please post a small reproducable example so we can figure it out.
FX
Yeah, I compile with the -ansi flag. Do I have any other options besides the two you mentioned?
Ree
No, that's pretty much it. Don't compile with `-ansi`.As a workaround, you can replace `_OUT_TO_STDERR` by its value 1, and ignore the warning (or copy the prototype in your own code).But really, don't compile with `-ansi` :-)
FX
OK, tried without the -ansi flag. Compiles fine, but the popup still appears. I'm calling the function in the first line of `main()`.
Ree