C3PO 0 Newbie Poster

When i use dlopen to dynamically load a library it seems i can not catch exceptions thrown by that library. As i understand it it's because dlopen is a C function.

Is there another way to dynamically load a library that makes it possible to catch exceptions thrown by the lib in GCC?

In Windows you can use LoadLibrary but for Linux i have only found dlopen but when using dlopen i can not catch exceptions.

I throw custom exceptions with its own namespace. I want to be able 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 that 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);

And mylib1 is dynamically linked.

This works in Windows (to catch my exceptions) but there I dont use dlopen of course.

The problem seems to be that mylib1 does not have the correct symbol information since objdump -TC mylib1.so | grep Exception1 returns nothing and objdump -TC mylib2.so | grep Exception1 returns the symbols. To build mylib1 i use -shared -fPIC -rdynamic -W1,--export-dynamic -W1,-soname,libMyLib.so My flags to build mylib2 is the same.

Maybe the problem is that i use an std::auto_ptr to point to MyNamspace::MyClass?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.