From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5246A3858408; Sun, 2 Jan 2022 11:36:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5246A3858408 From: "pierre.vigier at ymail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/102398] thread_local works incorrectly outside of main file Date: Sun, 02 Jan 2022 11:36:27 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pierre.vigier at ymail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 02 Jan 2022 11:36:28 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102398 Pierre Vigier changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |pierre.vigier at ymail dot= com --- Comment #1 from Pierre Vigier --- Hello, I am facing the same issue with the following program: a.h --- #pragma once struct A { static thread_local int x; }; a.cpp --- #include "a.h" thread_local int A::x =3D 0; main.cpp --- #include "a.h" int main() { A::x =3D 1; return 0; } When I compile with no optimization: x86_64-w64-mingw32-g++ -c -a.cpp -o a.obj x86_64-w64-mingw32-g++ -c main.cpp -o main.obj=20 x86_64-w64-mingw32-g++ main.obj a.obj -o test.exe The program runs fine. But, if I use optimizations: x86_64-w64-mingw32-g++ -c -O1 a.cpp -o a.obj x86_64-w64-mingw32-g++ -c -O1 main.cpp -o main.obj=20 x86_64-w64-mingw32-g++ -O1 main.obj a.obj -o test.exe The program crashes when it tries to access the thread local variable. x86_64-w64-mingw32-gcc -v: Using built-in specs. COLLECT_GCC=3Dx86_64-w64-mingw32-gcc COLLECT_LTO_WRAPPER=3D/usr/libexec/gcc/x86_64-w64-mingw32/11.2.1/lto-wrapper Target: x86_64-w64-mingw32 Configured with: ../configure --prefix=3D/usr --bindir=3D/usr/bin --includedir=3D/usr/include --mandir=3D/usr/share/man --infodir=3D/usr/shar= e/info --datadir=3D/usr/share --build=3Dx86_64-redhat-linux-gnu --host=3Dx86_64-redhat-linux-gnu --with-gnu-as --with-gnu-ld --verbose --without-newlib --disable-multilib --disable-plugin --with-system-zlib --disable-nls --without-included-gettext --disable-win32-registry --enable-languages=3Dc,c++,objc,obj-c++,fortran --with-bugurl=3Dhttp://bugzilla.redhat.com/bugzilla --enable-threads=3Dposix --with-isl --enable-libgomp --target=3Dx86_64-w64-mingw32 --with-sysroot=3D/usr/x86_64-w64-mingw32/sys-root --with-gxx-include-dir=3D/usr/x86_64-w64-mingw32/sys-root/mingw/include/c++ Thread model: posix Supported LTO compression algorithms: zlib gcc version 11.2.1 20210728 (Fedora MinGW 11.2.1-3.fc35) (GCC)=20 The same program was working fine on 10.3.1.=