Exceptions not getting when using shared c++ library

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
Last edited on
My original test app had test.cpp as:
1
2
3
4
5
6
#include <iostream>
#include <exception>
void only_function();
int main() {
   only_function();
}


I got the output wnen using libc++.so
Terminate called after throwing an instance of 'MyException'
what(): C++ Exception

I added the try catch in executable just to see that the exceptions were caught in executables
The following code works on my W7 machine:

test.cpp:
1
2
3
4
5
6
7
#include "test-lib.hpp"
#include <iostream>

int main()
{
    only_function();
}


test-lib.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "test-lib.hpp"
#include <iostream>

void only_function()
{
    try {
        throw MyException();
    } catch(MyException& e) {
        std::cout << "MyException caught from shared library\n"
                  << e.what() << '\n';
    } catch(std::exception& e) {
        //Other errors
    }
}


test-lib.hpp:
1
2
3
4
5
6
7
8
9
10
11
#include <exception>

void only_function();


struct MyException : public std::exception {
    const char * what () const noexcept
    {
        return "C++ Exception";
    }
};


Compiled this way:
g++ -std=c++17 -Wall -Wextra -pedantic-errors -fpic -c test-lib.cpp -o test-lib.o
g++ -shared test-lib.o -o test-lib.so
g++ -std=c++17 -Wall -Wextra -pedantic-errors test.cpp test-lib.so -o test.exe


Output:
MyException caught from shared library
C++ Exception


On Linux of course you need to follow its specific rules about library names.
Topic archived. No new replies allowed.