This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
collect2 / gcc-2.95.2 / HP-UX 10.20 / global C++ objects in sharedlibs
- To: gcc-bugs at gcc dot gnu dot org
- Subject: collect2 / gcc-2.95.2 / HP-UX 10.20 / global C++ objects in sharedlibs
- From: joe dot leherbauer at windriver dot com (Joe Leherbauer)
- Date: Mon, 27 Mar 2000 17:58:29 +0200
- Organization: TakeFive Software, a Wind River Company
collect2 on HP-UX never emits calls to {con,de}structors of global
C++ objects in shared libraries. An example is shown at the bottom.
I traced collect2 in gdb and found that the following patch solves
the problem:
*** gcc-2.95.2/gcc/collect2.c.orig Tue Oct 12 23:16:52 1999
--- gcc-2.95.2/gcc/collect2.c Tue Mar 21 17:20:24 2000
***************
*** 2349,2359 ****
{
case 1:
! if (which_pass != PASS_LIB)
! add_to_list (&constructors, name);
break;
case 2:
! if (which_pass != PASS_LIB)
! add_to_list (&destructors, name);
break;
--- 2349,2357 ----
{
case 1:
! add_to_list (&constructors, name);
break;
case 2:
! add_to_list (&destructors, name);
break;
Reproduce like this:
shell> uname -a
HP-UX gismo B.10.20 A 9000/820 2004661727 two-user license
shell> cat String.h
#pragma interface
class String {
const char * str;
public:
String(const char *s);
const char * GetStr();
};
extern String gString;
shell> cat String.cc
#pragma implementation
#include "String.h"
String gString ("global String");
String::String (const char * s)
: str(s)
{
}
const char *
String::GetStr()
{
return str;
}
shell> cat main.cc
#include <stdio.h>
#include "String.h"
int main ()
{
const char * s = gString.GetStr();
printf ("s = 0x%08X \"%s\"\n",
(unsigned long) s,
s ? s : "(null)");
return 0;
}
shell> gcc -v
Reading specs from /opt/gnu/lib/gcc-lib/hppa1.1-hp-hpux10.20/2.95.2/specs
gcc version 2.95.2 19991024 (release)
shell> gcc -fpic -c String.cc
shell> ld -b -o libbase.sl String.o
shell> gcc -c String.cc
shell> ar rv libbase.a String.o
shell> gcc -o main.static main.cc libbase.a
shell> gcc -o main.shared main.cc libbase.sl
shell> ./main.static
s = 0x00002B60 "global String"
shell> ./main.shared
s = 0x00000000 "(null)"