I have an executable test which is linked to a shared library libtest-lib.so.
All I am doing is throwing and catching an exception from the executable and calling a function of the shared library which also throws and catches an exception
The exception of the executable gets caught but the exception thrown in shared library is not getting caught.
Also when I build the shared library by linking it to static version of c++ library (i.e. libc++.a) instead of shared version (libc++.so), everything works normally.
I have already tried using the linker flag -shared-libgcc and -frtti but these do not work.
test.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <exception>
using namespace std;
struct MyException : public exception {
const char * what () const throw () {
return "C++ Exception";
}
};
void only_function();
int main() {
try {
throw MyException();
} catch(MyException& e) {
std::cout << "MyException caught from executable" << std::endl;
std::cout << e.what() << std::endl;
} catch(std::exception& e) {
//Other errors
}
only_function();
}
|
test-lib.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <exception>
using namespace std;
struct MyException : public exception {
const char * what () const throw () {
return "C++ Exception";
}
};
void only_function() {
try {
throw MyException();
} catch(MyException& e) {
std::cout << "MyException caught from shared library" << std::endl;
std::cout << e.what() << std::endl;
} catch(std::exception& e) {
//Other errors
}
}
|
Output when I built the shared library using libc++.so
MyException caught from executable
C++ Exception
terminate called after throwing an instance of 'MyException'
what(): C++ Exception
Abort
Output when I built the shared library using libc++.a
MyException caught from executable
C++ Exception
MyException caught from shared library
C++ Exception
Any help will be highly appreciated