A test program of mine loads a shared library (.so file). A function
call in the shared library throws an exception that I am trying to
catch in the main function of my test program. (I know that exception is being thrown for sure, I wrote the library to do that.)
However in the main program, the exception is not being caught. The flow of program goes past the catch block like no error has occurred. I am using g++ and I load the shared file using -l option. Only trying to load the program
statically I got the following error:
/usr/bin/ld: cannot find -lmy-shared-library
collect2: ld returned 1 exit status
Any idea why the exception is not getting caught. Searched around the
net but couldn't get much help. I posted this question on google group too but couldn't get any answers there.
The catch is indeed of correct type. The call is in the try block. Please do not assume any programatic mistakes on my side. Its the concept that I am trying to understand.
> Please do not assume any programatic mistakes on my side
That's the firs thing that you should check out.
> Its the concept that I am trying to understand.
¿what concept? There is no reason for exceptions to fail just because of the linking.
> I am throwing is std::logic_error and I am catching the same error.
¿is your program terminating after throwing `std::logic_error' ?
(maybe it crashes on another line)
One thing to keep in mind about throwing exceptions from shared libraries is that they should be compiled with the same compiler (same version, preferably) as the code that's catching them.
Yes, I am compiling the library with the same compiler. In fact, I wrote that library and my test program on the same machine and compiled them on the same machine. Compiled the library first into a .so file, installed it on my machine, and then begin compiling my main program.
I don't claim that it's impossible to throw an exception across dynamic library borders, but it's rather unlikely that it work.
The problem is that the throw needs to know where the catch is. It's like a function call from the dynamic library to the application. Right know i cannot imagine how that would be possible.