https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96853
Bug ID: 96853 Summary: Explicit template instantiation & thread_local interaction Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: tobias.bruell at gmail dot com Target Milestone: --- Compiling the below with > g++-10 main.cpp library.cpp -o main in gcc 10.1 leads to an executable "main" that segfaults. The problem seems to be that the out-commented "extern" declarations in library.hpp are missing. So, the program might be UB, but it would be nice to get an error message from the compiler (as Clang does). Otherwise, the linker could give an error: the final executable "main" contains a "callq 0x0" instruction (on x86_64) if you also add "-static" to the above command-line. I think the problem is that "_ZTHN7library3FooIiE1fE" (TLS init function for library::Foo<int>::f) is missing from any of the intermediate object files. -------------------------------- --------- library.hpp ---------- #ifndef INCLUDE_GUARD_LIBRARY_H_ #define INCLUDE_GUARD_LIBRARY_H_ namespace library { template<typename T> struct Foo { using Func = T (*) (T); static thread_local Func f; }; //extern template struct Foo<char>; //extern template struct Foo<int>; //extern template struct Foo<long>; } #endif ------------------------------- --------- library.cpp --------- #include "library.hpp" namespace library { template<typename T> T core_function (T val) { return val * 2; } template<typename T> thread_local typename Foo<T>::Func Foo<T>::f = &core_function<T>; template struct Foo<char>; template struct Foo<int>; template struct Foo<long>; } -------------------------------- ----------- main.cpp ----------- #include "library.hpp" int main () { return library::Foo<int>::f(2); }