From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 115707 invoked by alias); 4 Aug 2015 16:53:02 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 115697 invoked by uid 89); 4 Aug 2015 16:53:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_LOW,RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: smtp.ispras.ru Received: from smtp.ispras.ru (HELO smtp.ispras.ru) (83.149.199.79) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 04 Aug 2015 16:53:00 +0000 Received: from [10.10.3.121] (unknown [83.149.199.91]) by smtp.ispras.ru (Postfix) with ESMTP id 1B470214EC; Tue, 4 Aug 2015 19:52:57 +0300 (MSK) Date: Tue, 04 Aug 2015 16:53:00 -0000 From: Alexander Monakov To: Jonathan Wakely cc: Nikolay Vorobyov , gcc-help Subject: Re: Explicit instantiation and static objects in different modules In-Reply-To: Message-ID: References: <63FDE6C4-EFC2-44DD-991E-0BE895586162@gmail.com> User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-SW-Source: 2015-08/txt/msg00021.txt.bz2 On Tue, 4 Aug 2015, Jonathan Wakely wrote: > On 4 August 2015 at 16:00, Nikolay Vorobyov wrote: > > Is this a gcc's bug? > > I believe this is due to how symbol resolution works in DLLs on > Windows, where each DLL has its own copy of the static variable. I > don't know how to make it work as required by the C++ standard. One way would be to eliminate the inline definition of getInstance() in the header file, and move it into inst.cpp. If you inspect generated files with 'nm -C', you'll see that both libinst.dll and liba.dll have a definition for StaticObject::getInstance()::t. Jonathan, I think there might be a GCC bug here, but not what Nikolay originally meant. With -std=c++11, 'extern template' should prevent the compiler from instantiating methods of StaticObject, but it doesn't happen. Here's a minimal example: template struct S { static int bar() { return V; } }; extern template struct S<42>; int foo() { return S<42>::bar(); } Compile with g++ -std=c++11 -S -o- -Os and observe that 'foo' is optimized to 'return 42', although 'bar' should not have been instantiated. If you don't have a template class and make 'bar' itself a template function, GCC does not optimize 'foo', as expected. WDYT? Thanks. Alexander