https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91075
Bug ID: 91075
Summary: Wrong code generated for static variable with local
redeclaration
Product: gcc
Version: 9.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: andrey.vihrov at gmail dot com
Target Milestone: ---
Consider the following C++ sample:
static int x;
int main()
{
int x = 1;
{
extern int x;
return x;
}
}
Compiling the above code with "g++ test.cpp" gives:
/usr/bin/ld: /tmp/ccZhQ3uF.o: in function `main':
test.cpp:(.text+0x6): undefined reference to `x'
collect2: error: ld returned 1 exit status
Removing the first "int x;" declaration from main() fixes the problem.
According to Note 3.3.2.11 of [1], the local "extern int x;" declaration should
refer to the static variable from the global namespace.
The problem is reproducible with GCC 9.1.0 and all GCC versions / targets
available on gcc.godbolt.org, while clang and other compilers produce correct
code.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf