This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Dynamic libraries



Hello All.

I'm using RedHat5.0, glibc & binutils updated and EGCS-1.0.3.
I wrote a tiny C++ program that simply loads a (C++) dynamic library,
calls a function and unloads it. The problem is that if an exception
is thrown inside the library (and caught there) the program aborts.
The output is:

[alfa:~/tmp] app
Abort

In particular, if I comment out the "throw" line in the lib, everything
works well. Does somebody knows what's going on? Since I have no idea
about it, maybe I'm sending this to the wrong list--excuse me for that.

Please, c.c. the answer to me because I'm not a list member.

Code for both files follows, app.cpp is the application and lib.cpp is
the dynamic library.

Thank you very much,
-- Cassino

--- app.cpp --------------------------------------

#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv)
{
  void* lib = dlopen("./liblib.so", RTLD_NOW);
  if (!lib)
  {
    printf("DLFCN error: %s\n", dlerror());
    return 1;
  }
  void* f = dlsym(lib, "f");
  if (!f)
  {
    printf("DLFCN error: %s\n", dlerror());
    return 2;
  }
  ((void(*)(void))f)();
  dlclose(lib);
  return 0;
}

--- lib.cpp --------------------------------------

class Except
{
  public:
    int code; 
    Except(int c) : code(c) { }
};

static void g()
{
  throw Except(1);
}

void cpp_f()
{
  try {
    g();
  } catch (Except& ex) {
  }
}

extern "C" {
void f(void)
{
  cpp_f();
}
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]