GCC 4.8.1、C++11、共享库和异常处理问题

4

我的项目包括以下内容:

  • 我的程序,主要使用C++11编写(因此在C++03模式下编译它不现实)
  • 一个共享库(https://github.com/SOCI/soci),使用相同的编译器编译

SOCI抛出异常,我需要在我的代码中捕获。它曾经可以在GCC4.7.3上工作,但现在我已经迁移到GCC4.8.1,它就不能工作了:异常穿过所有处理程序(包括catch(...)),并导致终止:

terminate called after throwing an instance of 'soci::mysql_soci_error'
  what():  Table 'brphrprhprh' doesn't exist
The program has unexpectedly finished.

我尝试过:

  • throwing the same exception from my code (near the trouble point): it gets caught with the right handler;
  • recompiling SOCI with -std=c++11: no difference
  • adding __attribute__((visibility("default"))) to the exception class: no difference
  • fiddling with the -u option for typeinfo-related symbols: no difference in behaviour, symbols appear as undefined in nm output. Note that without -u's there are none at all:

    $ nm -D -C myprogram | grep soci | grep error
                     U soci::soci_error::soci_error(std::string const&)
    000000000044013a W soci::soci_error::~soci_error()
    0000000000440124 W soci::soci_error::~soci_error()
    0000000000440124 W soci::soci_error::~soci_error()
    00000000004c43b0 V typeinfo for soci::soci_error
                     U typeinfo for soci::mysql_soci_error
    00000000004c43d0 V typeinfo name for soci::soci_error
                     U typeinfo name for soci::mysql_soci_error
    00000000004c60c0 V vtable for soci::soci_error
                     U vtable for soci::mysql_soci_error
    
我也阅读了http://gcc.gnu.org/wiki/Visibility,但可能还缺少其他方面的知识。
有什么建议吗?
编辑 事实上,那并不是任何动态库问题。我应该立即尝试静态编译它 —— 这样做不会真正改变行为,但可以节省很多时间。 (查看答案)

你能用最小的示例重现它吗? - n. m.
@n.m. 我会尽力的。虽然我不确定它到底能有多“简洁”... - vines
1
一个主函数,一个其他函数,一个抛出,一个捕获,一个异常类,全部拆分成共享库和可执行文件。另外,nm -D 在你的共享库上显示了什么? - n. m.
你是否正在使用 dlopen 加载库? - n. m.
@n.m. 不,只是运行时链接。我目前卡在了复现问题上,示例代码要么按预期工作,要么无法链接,因此我尝试反映更多细节。 - vines
显示剩余2条评论
1个回答

4

最终我找出了问题... 唉。

问题并不是异常没有被捕获!而是当异常在析构函数中被抛出时,调用 std::terminate 是在 C++11 默认情况下不允许的。我真正面临的问题是这个: Destructors and noexcept -- 编译器的错误使我没有意识到库的错误...


1
FYI,SOCI bug已经修复 - https://github.com/SOCI/soci/pull/192 很抱歉之前没有及时处理。 - mloskot
1
是的,我之前已经fork了它,然后发现它已经在那里了 :) - vines

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接