我的项目包含共享库和 exe 客户端。我发现我自己从库中抛出的异常类没有被客户端 catch block 捕获,并且程序以“在抛出一个实例后调用终止...”消息终止。继续玩这个项目,我发现任何类型的异常都没有被捕获。例如,这个 catch 不起作用:
try { m_pSerialPort = new boost::asio::serial_port(m_IoService, "non-existing-port"); } catch(const boost::system::system_error& e) { // ... }
错误信息:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl <boost::exception_detail::error_info_injector <boost::system::system_error> >' what(): No such file or directory
GCC 版本为 4.4.1,Linux 操作系统。相同的代码在 Windows、MSVC 中成功运行。 什么原因可以阻止 GCC 程序正确捕获异常?
最佳答案
客户端 .exe 和共享库都应该与 libgcc 链接,以便跨越共享库边界。根据 GCC 手册:
... if a library or main executable is supposed to throw or catch exceptions, you must link it using the G++ or GCJ driver, as appropriate for the languages used in the program, or using the option -shared-libgcc, such that it is linked with the shared libgcc.
关于c++ - GCC 程序不捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2424836/