1

I'm new to mingw-w64 and I'm running into the following problem:

I recently installed MSYS on my Windows 10 computer according to the instructions provided in

How to install MinGW-w64 and MSYS2?

and I am currently trying to build some Win32 C programs. I first tried some simple programs and they seem to work; however, I ran into problems with the C abort function.

If I build the following program on Linux

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

int main(int argc, char *argv[]) {
  printf("bla bla bla\n");
  abort();
}

and later run it, I simply get the output

bla bla bla
Aborted

However, on Windows the output is

bla bla bla

This application has requested the Runtime to terminate it in an unusual
way. Please contact the application's support team for more information.

A message window also appears with the message

a.exe has stopped working -- A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.

Is this the way it is supposed to be? Anyway, I much prefer the Linux version.

Community
  • 1
  • 1
bostjanv
  • 11
  • 2

1 Answers1

3

abort is the same as raise(SIGABRT).

If you don't catch the SIGABRT signal, the default OS handler is invoked, on Linux this is dumping core, causing the shell to write Aborted on standard error.

On Windows, it terminates and the message you saw is displayed, on standard error if console subsystem is used or in a standard message box if gui. This can be suppressed by the non-standard _set_abort_behavior.

If you want a uniform action across systems, you need to register a SIGABRT handler and do something yourself:

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

void handle_abort(int sig)
{
   // do something (e.g write to log)
   // don't let this handler return if you want to suppress defaut handler
}

int main(void)
{    
    signal(SIGABRT, handle_abort);

    abort();
}
a3f
  • 8,517
  • 1
  • 41
  • 46
  • 1
    Note that the reason this suppresses the 'default message' is that the program no longer exits as a result of a signal — because you use `exit()` or `_exit()` or something similar. You can demonstrate the difference with a signal handler like this: `void handle_abort(int sig) { fprintf(stderr, "Abort signal (%d) caught by %s()\n", sig, __func__); signal(sig, SIG_DFL); raise(sig); /*exit(128+sig);*/ }` (or you can use `exit()` instead of `signal()` plus `raise()`). The 'default message' actually comes from the shell. – Jonathan Leffler May 04 '16 at 06:44
  • @JonathanLeffler You're right, corrected that part. – a3f May 04 '16 at 06:50