【问题标题】:clang try catch fails铿锵尝试捕捉失败
【发布时间】:2017-02-03 23:06:07
【问题描述】:

这是我所说的代码部分。

try {
       std::cerr << "first try" << std::endl;
       po::store(po::parse_config_file(ifs, _configFileOptions, false), vm);

} catch(...) {           
       std::cerr << "second try" << std::endl;            
}

只是为了寻求细节,我正在使用 boost program_options 来解析配置文件。 boost 引发了异常,因为我在文件中放置了一个无法识别的选项。

Clang 存在问题,无法捕获此异常。基本上我只在输出中看到

first try
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::unknown_option> >: unrecognised option 'TestFrequency'
Abort trap: 6

这是我的 clang 版本:

c++ --version
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

编辑:当没有异常时,解析和一切正常。

【问题讨论】:

  • 你有什么版本的 boost?也许你使用 catch(std::exception&) 会更好运
  • 提升 1.55。一切都是用 Clang 针对 libC++ 编译的。是的,我在提交问题之前尝试了 catch(std::exception&) 但它没有用。
  • 这在 boost 和 exe 针对不同的标准库编译时很常见(我知道您提到了这一点,但请再次检查)。
  • @RichardHodges 谢谢。我会仔细检查。但如果是这种情况,它不应该在链接时抛出错误吗?
  • 链接器不检查异常 ABI 兼容性。 C++ 需要对内部实现进行大量标准化,这会影响链接由不同编译器为 rtti、异常 ABI 和 mangle 构建的不同模块(现在,当您需要通过一个编译器重建所有库时,这很痛苦。希望他们会为这些东西带来一些标准,而不是只是语法糖。

标签: c++ exception-handling clang


【解决方案1】:

这可能与类似于this issue with GoogleMaps 或this question on SO 的RTTI(运行时类型信息)问题有关。

确保 Boost 和您的代码在编译时没有 fno-rtti 标志。

【讨论】:

    【解决方案2】:

    对我来说,解决方案是删除链接器标志“-Wl,-no_compact_unwind”(Catalina,Apple clang 版本 11.0.0 (clang-1100.0.33.17))

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多