This is the mail archive of the 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]

c++/7769: using static libraries sometimes loses static initialization

>Number:         7769
>Category:       c++
>Synopsis:       using static libraries sometimes loses static initialization
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu Aug 29 19:36:00 PDT 2002
>Release:        version 2.96 20000731
Mandrake Linux 8.1 2.96-0.62mdk
with binutils-
Red Hat Linux 7.3
with gcc-2.96-110 and binutils-
I instantiate a global/static variable call it "a" in one file (

I define main in another file (

Expected behavior: Constructor for "a" is called.

Actual behavior: Constructor for "a" is sometimes called, sometimes not, depending on linking and libraries.

Test 1:
  g++ 2 .o files
Result: WORKS - Constructor called

Test 2:
  g++ 2 .a files containing 1 .o file each
Result: FAILS - Constructor not called

Test 3:
  g++ 1 .a file containing 2 .o files
Result: FAILS - Constructor not called

Test 4:
  g++ 1 .a file containing 1 .o file (the .o file is obtained by concatenating and
Result: WORKS - Constructor called

There is other bizarre behavior in more complicated examples (not included).

In particular, this is puzzling because test 1 works - 2 .o files, and test 3 doesn't - 2 .o files inside a library.  Shouldn't it not matter whether the input comes from a inside a .a or on the command line?

CppUnit, an LGPL C++ unit test framework, uses static initializers to automatically register test suites with the test registry, simplifying test writing.  This is done using the CPPUNIT_TEST_SUITE_REGISTRATION macro, which evaluates to code similar to attached.  See
for more discussion.
#include <stdio.h>
class A {
    A() { printf("Hello world!\n"); }
A a;

int main() {}
untar attached file, make 
Content-Type: application/gzip; name="static_init.tar.gz"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="static_init.tar.gz"


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