开发者

Exceptions are not caught in GCC program

开发者 https://www.devze.com 2022-12-22 10:22 出处:网络
My project contains shared library and exe 开发者_运维知识库client. I found that my own exception class thrown from the library is not caught by client catch block, and program terminates with \"termi

My project contains shared library and exe 开发者_运维知识库client. I found that my own exception class thrown from the library is not caught by client catch block, and program terminates with "terminate called after throwing an instance of ..." message. Continuing to play with the project, I found that any exception of any type is not caught. For example, this catch doesn't work:

    try
    {
        m_pSerialPort = new boost::asio::serial_port(m_IoService, "non-existing-port");
    }
    catch(const boost::system::system_error& e)
    {
        // ...
    }

Error message:

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 version is 4.4.1, Linux OS. The same code is running successfully in Windows, MSVC. What reason can prevent GCC program to catch exceptions properly?


Both the client .exe and the shared library should to be linked with libgcc in order to throw across shared library boundaries. Per the GCC manual:

... 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号