https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83271
Bug ID: 83271
Summary: const variable previously declared "extern" results in
"weak declaration must be public" error
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: alexey.salmin at gmail dot com
Target Milestone: ---
The following code works well and produces a global read-only symbol:
extern const int x; // typically comes from a header file
const int x = 0;
But if you add a weak attribute, it fails:
extern const int x;
const int __attribute__((weak)) x = 0;
error: weak declaration of ‘x’ must be public
Even though "const" implies internal linkage in C++, the standard explicitly
makes an exception for names that were previously declared "extern" (see C++17
6.5 "Program and linkage"). The definition in question is already "public", it
should work without the "extern" specifier.
I've encountered this issue with g++ 6.4.0 20171026 (Debian 6.4.0-9) on
x86_64-linux-gnu. It seems to be reproducible with all g++ versions available
on godbolt.org (from 4.4 to 8.0 trunk).