【发布时间】:2017-10-11 12:25:51
【问题描述】:
我正在将一个项目从 MSVC 移植到 mingw。该项目包含 C 和 C++。然而,只要抛出异常,而不是被捕获,std::terminate 就会被调用并且应用程序崩溃。我不明白为什么会有任何建议。
我的工具链是安装在 Windows 中的 MSYS2 环境中的 cmake/ninja/mingw32。
MCVE:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(FailedExceptions)
add_executable(FailedExceptions c_funcs.c main.cpp)
//main.cpp
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
int main() {
try {
boost::property_tree::ptree pt;
std::printf("reading file\n");
boost::property_tree::read_xml("nonexistant-file", pt);
std::printf("provider file read\n");
} catch (...) {
std::printf("Exception caught\n");
}
return 0;
}
// c_funcs.c
int SomeCFunction()
{
return 0;
}
输出
$ cmake .. -GNinja
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: C:/msys64/mingw32/bin/cc.exe -- works
-- Check for working CXX compiler: C:/msys64/mingw32/bin/c++.exe -- works
-- Configuring done
-- Generating done
-- Build files have been written to: C:/msys64/home/sferguson/src/vis/build
$ ninja -v
[1/3] C:\msys64\mingw32\bin\cc.exe -MD -MT c_funcs.c.obj -MF c_funcs.c.obj.d -o c_funcs.c.obj -c ../c_funcs.c
[2/3] C:\msys64\mingw32\bin\c++.exe -MD -MT main.cpp.obj -MF main.cpp.obj.d -o main.cpp.obj -c ../main.cpp
[3/3] C:\msys64\mingw32\bin\c++.exe c_funcs.c.obj main.cpp.obj -o FailedExceptions.exe -Wl,--major-image-version,0,--minor-image-version,0 -lgcc_eh -lgcc_eh -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
$ ./FailedExceptions.exe
reading file
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
$
跟踪:
我可以从 Mingw 博士那里得到这个踪迹。确实看起来崩溃发生在异常的构造和实际抛出之间。
ntdll.dll!_NtTerminateProcess@8
ntdll.dll!_RtlExitUserProcess@4
kernel32.dll!_ExitProcessStub@4
msvcrt.dll!___crtExitProcess
msvcrt.dll!__cinit
msvcrt.dll!__exit
msvcrt.dll!_abort
FailedExceptions.exe!uw_init_context_1
FailedExceptions.exe!boost::property_tree::xml_parser::xml_parser_error::xml_parser_error
FailedExceptions.exe!boost::property_tree::xml_parser::read_xml<boost::property_tree::basic_ptree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >
FailedExceptions.exe!main
FailedExceptions.exe!__tmainCRTStartup [D:/develop/scripts/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c @ 334]
kernel32.dll!@BaseThreadInitThunk@12
ntdll.dll!___RtlUserThreadStart@8
ntdll.dll!__RtlUserThreadStart@8
疑难解答:
- 我发现一些 5-10 年前的帖子表明这可能是 mingw 的 dw2 和 sjlj 库之间的冲突,但我只安装了 mingw-w64-i686-gcc-libs 附带的
libgcc_s_dw2-1.dll二进制文件包在 msys pacman 存储库中。 - 我尝试更改我的 CMakeLists.txt 文件以使用
project(FailedExceptions LANGUAGES CXX)使用 C++ 编译所有内容。这可以防止 cmake 构建我的 C 文件。所以它确实适用于 MCVE,但我的整个项目缺少所有 C 内容。 - 我添加了
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -fexceptions),但似乎没有效果。我已通过ninja -v确认此标志已添加到 C 文件编译命令中。 - 从构建中删除 C 文件后,一切正常。但即使我没有在这个 MCVE 中使用 C 文件,我仍然在我的大项目中使用它。
- 我发现了另一个较小的例子here。如果我还在同一个项目中编译了一个 C 文件,我可以重现该问题。
【问题讨论】:
-
int SomeCFunction()在哪里调用? -
是
boost::property_tree::read_xml没有将流作为参数而不是字符串? -
@YSC int SomeCFunction() 不会在任何地方调用。它只是链接。这是这里奇怪的细节之一。我不需要调用 SomeCFunction() 来破坏 main.cpp 中的异常。我只需要链接到它。
-
@tobi303 存在
read_xml的重载,它接受const string&,然后打开具有该名称的文件。这就是我要调用的重载。见this -
由于 C 没有异常,可能的答案是每当抛出的 C++ 异常遇到来自 C 函数的堆栈帧时,它就会显式失败,因为无法正确传播异常。至少在早期版本的 gcc 中就是这种情况,其中需要显式编译选项来编译 C 代码并适当支持 C++ 异常。