https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92359
Bug ID: 92359 Summary: function static variable instantiated at -O1 despite extern template Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: sbence92 at gmail dot com Target Milestone: --- Host: all Target: all Build: all Created attachment 47169 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47169&action=edit minimal example In the below example func is inlined and function_local_static becomes a defined symbol when compiled with -O1 and above regardless of extern template class declaration. The problem is solved by adding -fno-inline to -O1 or -Og, but not with -O2 and above. Same can be observed with all gcc versions, clang 5.0+ (works with 4.0.1), icc, but not with msvc. This is problematic when one tries to rely on the extern template mechanism in shared libraries. The result is that function static variables get instantiated more than 1 times. Attached you find a minimal example showing exactly this. template <typename T> struct S { int func() { static int function_local_static = 0; return ++function_local_static; } }; extern template class S<int>; int user() { return S<int>().func(); }