Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Catching exceptions across shared object boundaries

Reply
Thread Tools

Catching exceptions across shared object boundaries

 
 
foxx
Guest
Posts: n/a
 
      08-29-2006
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)

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-29-2006
foxx wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Jens Theisen
Guest
Posts: n/a
 
      08-29-2006
foxx wrote:
> 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
 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      08-30-2006

foxx wrote:
> 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.

 
Reply With Quote
 
foxx
Guest
Posts: n/a
 
      08-31-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catching exceptions accross shared libraries Nindi C++ 4 12-23-2006 10:19 PM
catching exceptions across dlls =?Utf-8?B?U3JpZGhhcg==?= ASP .Net 1 04-27-2006 06:40 PM
Regexp help - Negative lookahead before across word boundaries Phrogz Ruby 2 02-19-2005 02:19 AM
Need Help Impersonating Across Process Boundaries hoochiegooch@hotmail.com ASP .Net 4 02-07-2005 04:40 PM
Credentials across multiple web service boundaries Bil Simser [MVP] ASP .Net Web Services 0 09-03-2004 11:23 PM



Advertisments