How to catch an exception thrown from a library in GCC C ++?

When I use dlopen to dynamically load a library, it seems I can not catch the exceptions thrown by this library. As I understand it, this is because dlopen is a function of C.

Is there any other way to dynamically load a library that allows catching exceptions thrown by lib in GCC?

On Windows you can use LoadLibrary, but for Linux I only found dlopen, but when using dlopen I cannot catch exceptions.

Edit : I tried void * handle = dlopen ("myLib.so", RTLD_NOW | RTLD_GLOBAL); and I still cannot catch the exceptions thrown by myLib.so

Edit 2 : I throw custom exceptions with my own namespace. I want to catch these exceptions outside the library. I want to be able to compile on different compilers, for example GCC 3.2 and GCC 4.1.

In myLib2.so, I throw exceptions, one example:

namespace MyNamespace {  
    void MyClass::function1() throw(Exception1) {  
        throw Exception1("Error message");  
    } 
}

In myLib1.so I want to catch this exception:

std::auto_ptr <MyNamespace::MyClass> obj = MyNamespace::getClass();
try {  
    obj->function1();  
} catch (MyNamespace::Exception1& e) {  
    std::cout << e.what();  //This is not caught for some reason.  
}

mylib1.so dynamically loads myLib2.so with

void* handle = dlopen("myLib2.so", RTLDNOW | RTLDGLOBAL);

This one works on Windows (to catch my exceptions), but there I do not use dlopen, of course.

Edit 3 : myLib1.so is dynamically linked.

+3
source share
2 answers

RTLD_GLOBAL dlopen. , info , ABI .

+8

GCC, .
, , "-fPIC" "-rdynamic".
RTLD_NOW - .

+2

Source: https://habr.com/ru/post/1722961/


All Articles