[Bug c++/77555] New: unused inline function in-function static variable accessed from outside leads to linker error
marmoo1024 at gmail dot com
gcc-bugzilla@gcc.gnu.org
Sun Sep 11 00:25:00 GMT 2016
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77555
Bug ID: 77555
Summary: unused inline function in-function static variable
accessed from outside leads to linker error
Product: gcc
Version: 6.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: marmoo1024 at gmail dot com
Target Milestone: ---
Created attachment 39603
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=39603&action=edit
examples showing the behavior.
Two part problem,
==== Part one
Unused inline functions can cause static variables to be instantiated by
constexpr variable taking the address of the static variable. The attached
test.cpp shows it working.
=== Part two ===
In-function static variables can be made visible outside, through templates,
and this causes a linker error. Not a compilation error.
test2.cpp:(.text._ZZN9someclass6unusedEvEN6getter3getEv[_ZZN9someclass6unusedEvEN6getter3getEv]+0x7):
undefined reference to `someclass::unused()::function_static'
The error happens with g++ 4.8.5, 5.3.0, 6.1.1, with -std=c++11, -std=c++14,
-std=c++1z. clang++-3.5 crashes, 3.8 and 3.9 compiles and objdump shows that
the in-function static variables are instantiated but the function is not
emitted (since it's implicitly inline). Any static variable that's not used is
not instantiated. The attached test2.cpp shows this. Naturally the behavior
doesn't change if the class is instantiated, and is not affected by
optimization, which I believe in gcc has extra control flow analysis. The only
difference to the error with optimization is that with O1 or above the unused
reference is from _GLOBAL__sub_I_main.
IMHO, gcc should follow the example of clang and instantiate static variables
if they're referenced. Definitely not a linker error, I don't know if part one
is required by the standard, but it seems plausible either way. If so, the
compiler should detect the use of such static variables and emit them.
More information about the Gcc-bugs
mailing list