Catching exceptions across shared object boundaries

F

foxx

This is driving me nuts. I can't catch an exception thrown out of a
..so library. It passes up and terminates the program instead. Here is
the code for the .so and the executabe, the compiler commands, and the
output. Please help, I ahve spent hours and hours on this!

====FILE libprint.c ==============================

#include <stdio.h>
#include <stdexcept>
using namespace std;

int hello() throw (runtime_error)
{
printf("hello world!\n");
throw runtime_error("kjh");
}

===FILE main.c ==================

#include <stdio.h>
#include <string>
#include <stdexcept>
using namespace std;

extern int hello() throw (runtime_error);

int main() {
try {
hello();
}
catch (...) {
printf("caught!\n");
}
}

===== COMMAND LINE ======

pinch.203$ g++ -fPIC -fexceptions -c libprint.c
pinch.204$ ld -G libprint.o -fexceptions -o libprint.so
pinch.205$ g++ main.c -fexceptions -lprint
pinch.206$ ./a.out
hello world!
terminate called after throwing an instance of 'std::runtime_error'
what(): kjh
Abort


(gcc version is 3.4.3)
 
V

Victor Bazarov

foxx said:
This is driving me nuts. I can't catch an exception thrown out of a
.so library. [..]

Just to let you know, .so and catching exceptions across them are not
defined by C++, so you might consider asking in the newsgroup for your
OS as well; folks there might be more accustomed to those things, and
have more experience (per newsreader capita) with shared objects.

V
 
J

Jens Theisen

foxx said:
This is driving me nuts. I can't catch an exception thrown out of a
.so library. It passes up and terminates the program instead. Here is
the code for the .so and the executabe, the compiler commands, and the
output. Please help, I ahve spent hours and hours on this!

The following does work:

g++ -fPIC -shared libprint.cc -o libprint.so

I'm not an expert, but I guess you're simply not creating a shared
library (whatever that means technically). -shared is also understood by ld.

Jens
 
E

Earl Purple

foxx said:
This is driving me nuts. I can't catch an exception thrown out of a
.so library. It passes up and terminates the program instead. Here is
the code for the .so and the executabe, the compiler commands, and the
output. Please help, I ahve spent hours and hours on this!

etc:

1. I'd use a makefile and build the library with -shared
2. Get rid of throw specifications.
 
F

foxx

Thanks guys -- for for reference, I got it working with syntax like
this:
(I'm using cygwin on windows, but compiling without cygwin
dependencies)

---------------Makefile:

libException.dll: Exception.cc
g++ -mno-cygwin -fPIC -shared -o libException.dll Exception.cc

libtest1.dll: test1.cc libException.dll
g++ -mno-cygwin -fPIC -shared -o libException.dll test1.cc

libmymain.dll: mymain.cc libtest1.dll libException.dll
g++ -mno-cygwin -fPIC -shared -o libmymain.dll mymain.cc libtest1.dll

main.exe: libmymain.dll
g++ -mno-cygwin -o main.exe main.cc -L. -ltest1 -lmymain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top