Catching exceptions across shared object boundaries

Discussion in 'C++' started by foxx, Aug 29, 2006.

  1. foxx

    foxx Guest

    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)
    foxx, Aug 29, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Aug 29, 2006
    #2
    1. Advertising

  3. foxx

    Jens Theisen Guest

    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
    Jens Theisen, Aug 29, 2006
    #3
  4. foxx

    Earl Purple Guest

    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.
    Earl Purple, Aug 30, 2006
    #4
  5. foxx

    foxx Guest

    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
    foxx, Aug 31, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    478
  2. =?Utf-8?B?U3JpZGhhcg==?=

    catching exceptions across dlls

    =?Utf-8?B?U3JpZGhhcg==?=, Apr 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    417
    Karl Seguin [MVP]
    Apr 27, 2006
  3. Nindi
    Replies:
    4
    Views:
    427
    Gianni Mariani
    Dec 23, 2006
  4. Bil Simser [MVP]

    Credentials across multiple web service boundaries

    Bil Simser [MVP], Sep 4, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    108
    Bil Simser [MVP]
    Sep 4, 2004
  5. Phrogz
    Replies:
    2
    Views:
    124
    William James
    Feb 19, 2005
Loading...

Share This Page