我在Windows 10上使用Cygwin
Mintty Terminal
和g++ 5.4.0
在http://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
。