c - Mingw-w64 上 C 中止函数的奇怪行为

标签 c windows-10 mingw-w64

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

我最近在我的 Windows 10 电脑上安装了 MSYS 中提供的说明

How to install MinGW-w64 and MSYS2?

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

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

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

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

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

bla bla bla
Aborted

但是,在 Windows 上输出是

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.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.

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

最佳答案

abortraise(SIGABRT) 相同。

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

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

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

#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 - Mingw-w64 上 C 中止函数的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37019889/

相关文章:

java - JNI 上的运行时

c - 指向同一位置的指针

c - 带参数函数调用后的栈结构

java - 最新版本的eclipse是否完全兼容windows 10?

opencv - 执行 Opencv 程序时系统崩溃

python - Windows : VIM doesn't find Python3 上的 GIT bash

c - 我不明白为什么限定符 static 在这里应用于 x?

visual-studio-2015 - Windows 10 App Store 提交需要哪些图像?

windows - 想要在 Windows 上移植现有的 rsync