为什么我的`set_terminate`没有被调用以及如何调用它?

时间:2017-03-26 23:54:57

标签: c++ g++ cygwin

我在Windows 10上使用Cygwin Mintty Terminalg++ 5.4.0http://www.cplusplus.com/reference/exception/set_terminate/上使用此示例。

// set_terminate example
#include <iostream>       // std::cerr
#include <exception>      // std::set_terminate
#include <cstdlib>        // std::abort

void myterminate () {
  std::cerr << "terminate handler called\n";
  abort();  // forces abnormal termination
}

int main (void) {
  std::set_terminate (myterminate);
  throw 0;  // unhandled exception: calls terminate handler
  return 0;
}

并使用g++ -o main main.cpp && ./main进行编译,但是没有打印任何内容。所以我假设我的处理程序没有被调用。它缺少什么?

如果我使用Visual Studio编译器编译它,它可以正常运行cl.bat /EHsc /Femain.exe main.cpp && ./main

$ cl.bat /EHsc /Femain.exe main.cpp && ./main
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

main.cpp
Microsoft (R) Incremental Linker Version 14.00.23506.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:main.exe
main.obj
terminate handler called

此处cl.bat是:

@echo off

:: Path to your Visual Studio folder.
::
:: Examples:
::     C:\Program Files\Microsoft Visual Studio 9.0
::     F:\VisualStudio2015
set VISUAL_STUDIO_FOLDER=F:\VisualStudio2015

:: Load compilation environment
call "%VISUAL_STUDIO_FOLDER%\VC\vcvarsall.bat"

:: Invoke compiler with any options passed to this batch file
"%VISUAL_STUDIO_FOLDER%\VC\bin\cl.exe" %*

我把它放在我的PATH变量上,以便能够从Cygwin环境调用Visual Studio编译器和工具。 Mintty Terminal

0 个答案:

没有答案