Question about shared library for Solaris

komiyama komiyama@sddc.sps.mot.com
Tue Apr 20 06:38:00 GMT 2004


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
-----------------------------------------------------------------------



More information about the Gcc-help mailing list