Subject: Constructor for global objects in shared lib not getting called
To: None <>
From: Dave Huang <>
List: tech-toolchain
Date: 12/05/1998 19:37:19
I'm running -current on an i386, with egcs as the compiler, although I
haven't tried this with gcc--it may have the same problem.

Suppose I have a class MyClass... if I create an instance of MyClass by
doing "MyClass c;" at the outermost level of my program (i.e. not in a
function), MyClass's constructor gets called when the program starts...

Now, if I take the definition of MyClass and the "MyClass c;" and put
them into a shared library, the global instance of MyClass still gets
created, but its constructor is never called. It's supposed to get
called, right? The latest Mozilla thinks so...

Sample code:

===== lib.cpp =====
#include <stdio.h>

struct MyClass
 MyClass() {

MyClass c;

void func()
  printf("c is at %p\n", &c);

===== main.cpp =====
#include <stdio.h>

void func(void);

int main(void)
 return 0;

% c++ -c -fPIC lib.cpp
% c++ main.cpp lib.o
% a.out
c is at 0xb0c0
% ar rc libsomelib_pic.a lib.o
% ld -Bshareable -Bforcearchive -o libsomelib_pic.a
% c++ main.cpp -lsomelib
% a.out
c is at 0x40025078

Name: Dave Huang     |   Mammal, mammal / their names are called /
INet:   |   they raise a paw / the bat, the cat /
FurryMUCK: Dahan     |   dolphin and dog / koala bear and hog -- TMBG
Dahan: Hani G Y+C 23 Y++ L+++ W- C++ T++ A+ E+ S++ V++ F- Q+++ P+ B+ PA+ PL++