This is the mail archive of the gcc-help@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]
Other format: [Raw text]

RE: Question about shared library for Solaris


As Les said, use g++ when linking C++ code.  However, note that there
are problems on Solaris and Linux (those are the platforms that I have
experience on - don't know if the problem exists elsewhere) passing and
catching exceptions across library boundaries when building with GCC
2.95.X.  Linking with g++ with takes care of some of the problems, but
you may still run into cases where the exceptions don't get caught and
the program aborts.  I would highly recommend upgrading to a more recent
version of GCC if possible.  That has fixed all of the problems we have
seen like this.  However, you still need to link with g++.

Cheers,
Lyle

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of komiyama
Sent: Tuesday, April 20, 2004 12:39 AM
To: gcc-help@gcc.gnu.org
Subject: Question about shared library for Solaris


Hi GCC experts,

I have a question about Shared library that compiled with Gcc for
Solaris.

With above example code,

---------- test1.h -----------------------------------------------
class class1 {

  public:
   ~class1 ();

   void func1 ();

};
---------- test1.cc ----------------------------------------------
#include <stdexcept>
#include "test1.h"

using namespace std;

void class1::func1 () {

   throw runtime_error ("This is erro ");

}


class1::~class1 () {

   cout << "Class1 destructor is called " << endl;

}
--------- main.cc -------------------------------------------------
#include "test1.h"
#include <stdexcept>

using namespace std;

class except_1 {};

class class2 {

public:
   ~class2 () {
     cout << "Class2 destructor is called "<< endl;
   }

   void func1() { throw except_1 (); }


   void func2 () { throw runtime_error ("Error from class2"); }

};

int main () {

   class2 test2;

   try {
     test2.func1();
   }
   catch ( except_1& e ) {
     cout << "Catch except1 " << endl;
   }


   try {
     test2.func2();
   }
   catch ( runtime_error& e ) {
     cout << "Catch except2 " << e.what() << endl;
   }

   try {                               // This try block ignored (1)
     class1 test;
     test.func1();
   }
   catch ( runtime_error& e ) {
     cout << "Catch except2 "<< e.what () << endl;
   }
}
-------------------------------------------------------------------

When I create executable with Makefile1, the generated executable
is crashed with,


Catch except1
Catch except2 Error from class2
Abort                                <----

It seems that try catch block (1) seems to ignored.

---------- Makefile1 ----------------------------------------------

SHARE_OBJ = test1.o


.cc.o :
	g++ -g -fPIC -c -o $@ -save-temps $<


libshared.so: ${SHARE_OBJ}
	/usr/ccs/bin/ld  -G -o $@ ${SHARE_OBJ}

test: main.o libshared.so
	g++ -v -o $@ $< -L. -L/usr/local/gcc/2.95.3/lib -lshared
-lstdc++

clean:
	-/bin/rm *.o *.so

-----------------------------------------------------------------------

But when I compiled with Makefile2, generated executable run without
any problems.

The only difference is when created shared library, in the failed case,
it directory called /usr/ccs/bin/ld ,but passed case it called
/usr/ccs/bin/ld
through g++ compiler driver.

( It seems that g++ called collect2 and collect2 called /usr/ccs/bin/ld)

So my questions are,
1: What does collect2 do when it passed data to /usr/ccs/bin/ld ?
2: Is it possible to work with only /usr/ccs/bin/ld ?
     ( Does not use g++ compiler driver when create shared object )

Environemnt is
   O.S : Solaris8 (Sparc)
   Gcc : 2.95.3


-------------------- Makefile2 ----------------------------------------
SHARE_OBJ = test1.o


.cc.o :
	g++ -g -fPIC -c -o $@ -save-temps $<


libshared.so: ${SHARE_OBJ}
	g++ -v -G -o $@ ${SHARE_OBJ} -lc

test: main.o libshared.so
	g++ -v -o $@ $< -L. -L/usr/local/gcc/2.95.3/lib -lshared
-lstdc++

clean:
	-/bin/rm *.o *.so

----------------------------------------------------------------------


Takeo Komiyama

-----------------------------------------------------------------------
       Takeo Komiyama
      Voice : +81-22-377-9767 Fax : +81-22-377-9709
      ESLD design , CSG
      Semiconductor Products Division , Nippon Motorola Ltd.
      2-9-1 , Akedori, Izumiku, Sendai-shi, Miyagi-ken 981-32
         Email :    komiyama@sddc.sps.mot.com
-----------------------------------------------------------------------


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