2017-10-11 88 views
2

我正在将一个项目从MSVC移植到mingw。该项目包含C和C++。然而,每当抛出一个异常,而不是被捕获,std :: terminate被调用,应用程序崩溃。我不明白为什么如此任何意见将不胜感激。混合C和C++导致异常终止

我的工具链是安装在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. 
$ 

跟踪:

我可以从Dr. Mingw那里得到这个痕迹。它确实确实出现了崩溃发生在异常构建和实际投掷之间。

[email protected] 
[email protected] 
[email protected] 
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] 
[email protected]@12 
[email protected] 
[email protected] 

故障排除:

  • 我发现一些帖子来自5-10年前这表明这可能是MinGW的的DW2和sjlj库之间有冲突,但我只安装了libgcc_s_dw2-1.dll二进制文件,其配备msys pacman存储库中的mingw-w64-i686-gcc-libs软件包。
  • 我试着改变我的CMakeLists.txt文件来编译一切与C++ project(FailedExceptions LANGUAGES CXX)。这可以防止cmake建立我的C文件。所以它对MCVE有效,但是我的完整项目缺少所有的C内容。
  • 我已经加了set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -fexceptions)但它似乎没有效果。我已用ninja -v确认此标志已添加到C文件编译命令中。
  • 一旦我从构建中删除C文件,一切正常。但即使我没有在这个MCVE中使用C文件,我仍然在我的大项目中使用它。
  • 我发现了另一个更小的例子here。我可以重现那个问题IFF我也在同一个项目中编译一个C文件。
+0

哪里是'INT SomeCFunction()'叫什么名字? – YSC

+1

是'boost :: property_tree :: read_xml'没有将流作为参数而不是字符串? – user463035818

+1

@YSC int SomeCFunction()不会在任何地方调用。它是简单的链接。这是这里的一个奇怪的细节。我不需要调用SomeCFunction()来销毁main.cpp中的异常。我只需要链接到它。 – Stewart

回答

1

这是一个feature(bug?) cmake隐式库检测引入cmake 3.1。 CMake认为在C模式下GCC需要链接gcc_eh,这会打破C++异常处理。

您可以通过将其添加到CMakeLists来禁用隐式库检测。TXT:

set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "") 

(不知道如何做到这gcc_eh从列表中排除)

+0

事实证明,当我使用这个命令时,'gcc_eh'是唯一被排除的库。其他的都包含在CXX链路中,所以这个解决方案是完美的。 – Stewart

0

RustyX的答案是公认的,但我也发现了变通的另一个号码,似乎工作:

  • set_source_files_properties(filename.c PROPERTIES LANGUAGE CXX)
  • set(CMAKE_C_COMPILER /path/to/cpp/compiler)
  • set(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS c)

但RustyX的是最好的答案为止。

编辑:最后一个不工作。它仍然使用C编译器构建。我还需要从CMAKE_C_SOURCE_FILE_EXTENSIONS中删除c