6

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?

6
  • Could you kindly post the Makefile so we can see what arguments you're passing to gcc? You can pass arguments which disable exceptions. Commented Mar 11, 2010 at 12:34
  • Additional information: when I add this code: try {throw 1;}catch(int){} to the client executable, everything is OK. But the same code in the shared library gives: terminate called after throwing an instance of 'int' Commented Mar 11, 2010 at 13:01
  • Conspicuous Compiler: technical question. How can I post Makefile here? Comment is restricted by length and doesn't allow any formatting. Commented Mar 11, 2010 at 13:03
  • In case you're seeing this behavior when using threads, check my similar issue here: stackoverflow.com/questions/1377833/…. Might be a long shot, but worth verifying Commented Mar 11, 2010 at 13:06
  • After changing -static switch to -static-libgcc in the shared library program seems to be working. I don't understand what is side effect of this change. Is my library still statically linked to third-party library? Anyway, this is the direction. Commented Mar 11, 2010 at 13:16

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Just reading this place in the GCC manual :) I am trying to link third-party libraries statically, it looks like I need to learn this issue better. The program is working now, thank you.
You can link to the third-party libraries statically, just make sure the third party libraries link to libgcc dynamically. Thus you will have only one instance of libgcc in your process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.