public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102398] New: thread_local works incorrectly outside of main file
@ 2021-09-18 11:19 marcelotpcruz at gmail dot com
  2022-01-02 11:36 ` [Bug c++/102398] " pierre.vigier at ymail dot com
  0 siblings, 1 reply; 2+ messages in thread
From: marcelotpcruz at gmail dot com @ 2021-09-18 11:19 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/102398] thread_local works incorrectly outside of main file
  2021-09-18 11:19 [Bug c++/102398] New: thread_local works incorrectly outside of main file marcelotpcruz at gmail dot com
@ 2022-01-02 11:36 ` pierre.vigier at ymail dot com
  0 siblings, 0 replies; 2+ messages in thread
From: pierre.vigier at ymail dot com @ 2022-01-02 11:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102398

Pierre Vigier <pierre.vigier at ymail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pierre.vigier at ymail dot com

--- Comment #1 from Pierre Vigier <pierre.vigier at ymail dot com> ---
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 = 0;

main.cpp
---
#include "a.h"

int main()
{
    A::x = 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 
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 
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=x86_64-w64-mingw32-gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-w64-mingw32/11.2.1/lto-wrapper
Target: x86_64-w64-mingw32
Configured with: ../configure --prefix=/usr --bindir=/usr/bin
--includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info
--datadir=/usr/share --build=x86_64-redhat-linux-gnu
--host=x86_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=c,c++,objc,obj-c++,fortran
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-threads=posix
--with-isl --enable-libgomp --target=x86_64-w64-mingw32
--with-sysroot=/usr/x86_64-w64-mingw32/sys-root
--with-gxx-include-dir=/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) 

The same program was working fine on 10.3.1.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-02 11:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-18 11:19 [Bug c++/102398] New: thread_local works incorrectly outside of main file marcelotpcruz at gmail dot com
2022-01-02 11:36 ` [Bug c++/102398] " pierre.vigier at ymail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).