From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14257 invoked by alias); 30 Aug 2002 02:36:01 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 14222 invoked by uid 71); 30 Aug 2002 02:36:00 -0000 Resent-Date: 30 Aug 2002 02:36:00 -0000 Resent-Message-ID: <20020830023600.14221.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, hablack@vt.edu Received: (qmail 13017 invoked by uid 61); 30 Aug 2002 02:29:36 -0000 Message-Id: <20020830022936.13016.qmail@sources.redhat.com> Date: Thu, 29 Aug 2002 20:16:00 -0000 From: hablack@vt.edu Reply-To: hablack@vt.edu To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version: gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: c++/7769: using static libraries sometimes loses static initialization X-SW-Source: 2002-08/txt/msg00622.txt.bz2 List-Id: >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 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