https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102398
Bug ID: 102398 Summary: thread_local works incorrectly outside of main file Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: marcelotpcruz at gmail dot com Target Milestone: --- Created attachment 51479 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51479&action=edit thread_local_work_at_main_fails_wherever Hello, This bug is related to: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81915 I believe above link can be closed as it's old and the specification is a bit incorrect. The problem is not about it being inline or inside a class var, as any of those works if such are inside the file with the main(). Real problem is about variable definition outside of "main" file, so when it's only declared, the problem doesn't show, that's why inline reports the bug, it tells the compiler to define on the same line as it's defined. - - - - - When using thread_local in main file it works! -------------- Clean: Debug in thread_local (compiler: GNU GCC Compiler)--------------- Cleaned "thread_local - Debug" -------------- Build: Debug in thread_local (compiler: GNU GCC Compiler)--------------- g++.exe -Wall -fexceptions -g -O -save-temps -c C:\my_codes\thread_local\main.cpp -o obj\Debug\main.o g++.exe -o bin\Debug\thread_local.exe obj\Debug\main.o Output file is bin\Debug\thread_local.exe with size 152.48 KB Process terminated with status 0 (0 minute(s), 1 second(s)) 0 error(s), 0 warning(s) (0 minute(s), 1 second(s)) - - - - - When using thread_local outside of main file, throught #include including , it fails: -------------- Clean: Debug in thread_local_fail (compiler: GNU GCC Compiler)--------------- Cleaned "thread_local_fail - Debug" -------------- Build: Debug in thread_local_fail (compiler: GNU GCC Compiler)--------------- g++.exe -Wall -fexceptions -g -O -save-temps -c C:\my_codes\thread_local_fail\extern_thread_local.cpp -o obj\Debug\extern_thread_local.o g++.exe -Wall -fexceptions -g -O -save-temps -c C:\my_codes\thread_local_fail\main.cpp -o obj\Debug\main.o g++.exe -o bin\Debug\thread_local_fail.exe obj\Debug\extern_thread_local.o obj\Debug\main.o c:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj\Debug\main.o:C:/Users/MarceloTPC/Documents/my_codes/thread_local_fail/extern_thread_local.cpp:4: multiple definition of `TLS init function for does_not_work_string_located_at_main[abi:cxx11]'; obj\Debug\extern_thread_local.o:C:/Users/MarceloTPC/Documents/my_codes/thread_local_fail/extern_thread_local.cpp:4: first defined here c:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj\Debug\main.o:main.cpp:(.data+0x0): multiple definition of `__emutls_v._Z36does_not_work_string_located_at_mainB5cxx11'; obj\Debug\extern_thread_local.o:extern_thread_local.cpp:(.data+0x0): first defined here collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minute(s), 1 second(s)) 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) - - - - - Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/11.2.0/lto-wrapper.exe OFFLOAD_TARGET_NAMES=nvptx-none Target: x86_64-w64-mingw32 Configured with: ../configure --prefix=/R/winlibs64_stage/inst_gcc-11.2.0/share/gcc --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-offload-targets=nvptx-none --with-pkgversion='MinGW-W64 x86_64-posix-seh, built by Brecht Sanders' --with-tune=generic --enable-checking=release --enable-threads=posix --disable-sjlj-exceptions --disable-libunwind-exceptions --disable-serial-configure --disable-bootstrap --enable-host-shared --enable-plugin --disable-default-ssp --disable-rpath --enable-libstdcxx-pch --enable-libstdcxx-time=yes --disable-libstdcxx-debug --disable-version-specific-runtime-libs --with-stabs --disable-symvers --enable-languages=c,c++,fortran,lto,objc,obj-c++,d,jit --disable-gold --disable-nls --disable-stage1-checking --disable-win32-registry --disable-multilib --enable-ld --enable-libquadmath --enable-libada --enable-libssp --enable-libstdcxx --enable-lto --enable-fully-dynamic-string --enable-libgomp --enable-graphite --enable-mingw-wildcard --with-mpc=/d/Prog/winlibs64_stage/custombuilt --with-mpfr=/d/Prog/winlibs64_stage/custombuilt --with-gmp=/d/Prog/winlibs64_stage/custombuilt --with-isl=/d/Prog/winlibs64_stage/custombuilt --enable-install-libiberty --enable-__cxa_atexit --without-included-gettext --with-diagnostics-color=auto --enable-clocale=generic --with-libiconv --with-system-zlib --with-build-sysroot=/R/winlibs64_stage/gcc-11.2.0/build_mingw/mingw-w64 CFLAGS=-I/d/Prog/winlibs64_stage/custombuilt/include/libdl-win32 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.2.0 (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) Windows 10 Home - 20H2