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

hablack@vt.edu hablack@vt.edu
Thu Aug 29 19:36:00 GMT 2002


>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
>Closed-Date:
>Last-Modified:
>Originator:     hablack@vt.edu
>Release:        version 2.96 20000731
>Organization:
>Environment:
Mandrake Linux 8.1 2.96-0.62mdk
with binutils-2.11.90.0.8-5mdk
ALSO
Red Hat Linux 7.3
with gcc-2.96-110 and binutils-2.11.93.0.2-11
>Description:
I instantiate a global/static variable call it "a" in one file (one.cc)

I define main in another file (two.cc)

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 one.cc and two.cc)
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?

Relevance:
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 http://cppunit.sourceforge.net/cppunit_cookbook.html#cppunit_cookbook
for more discussion.

one.cc:
#include <stdio.h>
class A {
public:
    A() { printf("Hello world!\n"); }
};
A a;

two:cc
int main() {}
>How-To-Repeat:
untar attached file, make 
>Fix:
n/a
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/gzip; name="static_init.tar.gz"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="static_init.tar.gz"

H4sIAKjSbj0AA+2WzW7bMAzHc42egssaoE0RV7IkG3CKYbntsjcYViiKswhzrMJW1kORd59k16mL
dcslTVeUv4NMkNQHTP0l1U45o29MadzV4IWgVNA0lf5LWSpZ/9sxoKnkXEoWcz6gjEnvAvlSC+qz
rZ2qAAZrVfwz71D8jVL36v9V/cxXpsiPPQdllCaJ+Gv9JU979fd7gXHmXUCPvZDneOf1d3ntso0y
JYPQxk3Lm1aQYXTVhDoj7gzeGYKMI5uNI63J8MflJUw1nF2TwizGkfJuS4b+51a/4Oxz8C+sW3u/
LfPIgruzT+PfSTPZ03AYtOeAqYV2Sc16Mj9TiCrwRkhQbY8/3F2/uOnHs3YlbXJrdxm8yRBhZG03
C1Pmy96oj64uXxBd5KrMyLDawHQFEx+aRG1sQl67vAfp6z/8Mq2PP8ch/bOYPehfJIz6e4LFMmGo
/1Pw0ZS62C5zuK7d0tho/Yn4Da3qGuZwT263i8Jov7fn5xdwD7eVKd3qfPQlLwoLd7Yqlh++laOL
GezIbkbmoGb//45H+vT1H07K19A/ZUmnf5HEIug/iSnq/xR4QTdXVdD3DsX77ujrf/+4OfIhcFD/
Quzvf8pZeP/LJEb9n4L9/T9qX38j8uhp74MRngoIgiAIgiAIgiAIgiAIgiAI8sb4DXGkm0QAKAAA



More information about the Gcc-bugs mailing list