Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions between shared C++ libraries in g++

As far as I know it is not defined (undefined behaviour) in C++STD how compiled application should handle situations when one shared library raises exception inside another one [shared lib]. The code produced by G++/Linux shows that it is possible to raise exceptions between shared libraries.

My questions are:

  1. Is it portable (MSVS) ?
  2. In which cases shared library A will not be able to handle an exception from shared library B? [I mean the application would crash; library A knows about possible exceptions from B].
  3. Does exception handling behaviour in my example depend on linker?
like image 669
barankin Avatar asked Jul 28 '11 05:07

barankin


2 Answers

That really depends on your use of shared libraries. If you use them as physical modules within a same application/system and compile them with the same compiler, you should be OK (as long as you also dynamically link to the C++ standard library).

However, if there is a possibility that a shared library is compiled with one compiler (including another version of the same compiler) and accessed by a module compiled by another one, you are in trouble: there is no standard binary compatibility interface for exceptions.

like image 55
Nemanja Trifunovic Avatar answered Sep 28 '22 18:09

Nemanja Trifunovic


This seems to be a compiler-dependent issue. I have had problems with that on deploying to Windows, where the MinGW-GCC has to be built with a shared libstdc++ to enable cross-DLL exception handling (which is non-default for the Debian version and required me to re-compile the GCC).

Thus I assume there is no common ABI between compilers, since the GCC doesn't even conform with itself in all respects. With respect to technical considerations, the low-level exception handling information needs to be stored somewhere, and this somewhere is likely to be a third library C, which does handle exceptions. Compilers might be able to share this library, but I assume they aren't.

On the bright side: I am currently cross-compiling a fairly large project for POSIX systems and Windows and heavily using cross-library exceptions in a multi-threaded environment. It works with a shared libstdc++ for the GCC, and since all components are open source, I had the luxury to simply recompile all C++ dependencies for the MinGW.

like image 30
thiton Avatar answered Sep 28 '22 19:09

thiton