From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15827 invoked by alias); 15 Aug 2012 21:23:09 -0000 Received: (qmail 15812 invoked by uid 22791); 15 Aug 2012 21:23:07 -0000 X-SWARE-Spam-Status: No, hits=-3.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_BJ,TW_CX,TW_DC,TW_GX,TW_OV,T_FRT_COCK X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 15 Aug 2012 21:22:55 +0000 From: "travis at gockelhut dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/54276] New: Lambda in a Template Function Undefined Reference to local static Date: Wed, 15 Aug 2012 21:23:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: travis at gockelhut dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-08/txt/msg00988.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54276 Bug #: 54276 Summary: Lambda in a Template Function Undefined Reference to local static Classification: Unclassified Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned@gcc.gnu.org ReportedBy: travis@gockelhut.com Created attachment 28021 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28021 Compile The following code will fail to link with "undefined reference to `x'" when compiled with g++ (4.6.1, 4.6.2, 4.6.3 and 4.7.0): template void foo(T) { static int x = 1; auto f = [] { return x + 1; }; f(); } --- /tmp/cc236Klq.o: In function `void foo(int)::{lambda()#1}::operator()() const': unmangled-ref.cpp:(.text+0x20): undefined reference to `x' collect2: error: ld returned 1 exit status --- It *appears* we attempt to load x from the unmangled version: movl _ZL1x(%rip), %eax In g++ 4.6.3, you can work around this issue by explicitly capturing the static variable: auto f = [&x] { return x + 1; }; So x is loaded from the fully-mangled name: movq $_ZZ3fooIiEvT_E1x, -16(%rbp) --- $> g++ -v Using built-in specs. COLLECT_GCC=g++-4.7 COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.0-7ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --disable-bootstrap --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.7.0 (Ubuntu/Linaro 4.7.0-7ubuntu3)