Le lun. 15 janv. 2024 à 22:57, Martin Storsjö <mar...@martin.st> a écrit : > > On Thu, 11 Jan 2024, Antonin Décimo wrote: > > > It is sufficient to specify the section on the symbol declaration. > > > > Signed-off-by: Antonin Décimo <anto...@tarides.com> > > --- > > mingw-w64-libraries/winpthreads/src/thread.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/mingw-w64-libraries/winpthreads/src/thread.c > > b/mingw-w64-libraries/winpthreads/src/thread.c > > index 245bc52a6..a0bd711d1 100644 > > --- a/mingw-w64-libraries/winpthreads/src/thread.c > > +++ b/mingw-w64-libraries/winpthreads/src/thread.c > > @@ -533,8 +533,9 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, > > LPVOID lpreserved) > > # pragma section(".CRT$XLF", long, read) > > #endif > > > > -extern const PIMAGE_TLS_CALLBACK > > WINPTHREADS_ATTRIBUTE((WINPTHREADS_SECTION(".CRT$XLF"))) __xl_f; > > -const PIMAGE_TLS_CALLBACK > > WINPTHREADS_ATTRIBUTE((WINPTHREADS_SECTION(".CRT$XLF"))) __xl_f = > > __dyn_tls_pthread; > > +WINPTHREADS_ATTRIBUTE((WINPTHREADS_SECTION(".CRT$XLF"))) > > +extern const PIMAGE_TLS_CALLBACK __xl_f; > > +const PIMAGE_TLS_CALLBACK __xl_f = __dyn_tls_pthread; > > Why do we need to declare the variable beforehand here at all?
The idea of declaring first as extern const was from code in Google Chrome (but note that the code is C++, not C): // When defining a const variable, it must have external linkage to be sure the // linker doesn't discard it. extern const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry; const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry = on_callback; https://chromium.googlesource.com/chromium/src/+/lkgr/base/win/dllmain.cc#70 > Do we build the code with warnings enabled for defining symbols without them > being > declared before? Indeed, clang-cl has such a warning. It's also possible that the warning originated in the C++ world and found its way to C. Curiously, clang-cl doesn't warn if I remove the declaration in misc.c, but it does in thread.c. src/thread.c(542,27): error: no previous extern declaration for non-static variable '__xl_f' [-Werror,-Wmissing-variable-declarations] 542 | const PIMAGE_TLS_CALLBACK __xl_f = __dyn_tls_pthread; | ^ src/thread.c(542,7): note: declare 'static' if the variable is not intended to be used outside of this translation unit 542 | const PIMAGE_TLS_CALLBACK __xl_f = __dyn_tls_pthread; | ^ 1 error generated. libtool: compile: clang-cl.exe -DHAVE_CONFIG_H -I. -I./include -DIN_WINPTHREAD -DWINPTHREAD_DBG=1 -D_CRT_NONSTDC_NO_WARNINGS -Wall -DWIN32_LEAN_AND_MEAN -nologo -clang:-std=gnu17 -fansi-escape-codes -clang:-Wall -WX -clang:-Wno-reserved-identifier -clang:-Wno-nonportable-system-include-path -clang:-Wno-unsafe-buffer-usage -clang:-Wno-language-extension-token -clang:-Wno-declaration-after-statement -Wno-unused-macros -Wno-extra-semi-stmt -wd4324 -wd4820 -wd5045 -c -showIncludes src/thread.c https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-variable-declarations (I may have been overzealous on the warning list!) I'll add some info to the commit message. What about simply: > Declaring the variable as extern const may help prevent the linker from > discarding the symbol, and prevents a compiler warning. _______________________________________________ Mingw-w64-public mailing list Mingw-w64-public@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mingw-w64-public