C abort 函数在 mingw-w64 上的奇怪行为 [英] Strange behavior of C abort function on mingw-w64

查看:90
本文介绍了C abort 函数在 mingw-w64 上的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 mingw-w64 的新手,但遇到了以下问题:

我最近根据 Windows 10 计算机安装了 MSYS

中提供的说明

如何安装 MinGW-w64 和 MSYS2?>

我目前正在尝试构建一些 Win32 C 程序.我首先尝试了一些简单的程序,它们似乎有效;但是,我遇到了问题C 中止函数.

如果我在 Linux 上构建以下程序

#include #include int main(int argc, char *argv[]) {printf("bla bla bla\n");中止();}

然后运行它,我只是得到输出

bla bla bla中止

然而,在 Windows 上输出是

bla bla bla此应用程序已请求运行时在异常情况下终止它办法.请联系应用程序的支持团队了解更多信息.

一个带有消息的消息窗口也会出现

<块引用>

a.exe 停止工作 --一个问题导致程序停止正常工作.窗户将关闭程序并通知您是否有可用的解决方案.

这是应该的方式吗?无论如何,我更喜欢 Linux 版本.

解决方案

abortraise(SIGABRT) 相同.

如果你没有捕捉到 SIGABRT 信号,默认的 OS 处理程序被调用,在 Linux 上这是转储核心,导致 shell 在标准错误时写入 Aborted.

在 Windows 上,它会终止并显示您看到的消息,如果使用控制台子系统,则显示为标准错误,如果使用 gui,则显示在标准消息框中.这可以通过非标准的 _set_abort_behavior 来抑制.

如果你想要跨系统的统一操作,你需要注册一个 SIGABRT 处理程序并自己做一些事情:

#include #include #include void handle_abort(int sig){//做一些事情(例如写入日志)//如果你想取消默认处理程序,不要让这个处理程序返回}int main(void){信号(SIGABRT,handle_abort);中止();}

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.

解决方案

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();
}

这篇关于C abort 函数在 mingw-w64 上的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆