https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91476

            Bug ID: 91476
           Summary: const reference variables sharing the same name in two
                    anonymous namespaces cause a multiple definition error
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: abbeyj+gcc at gmail dot com
  Target Milestone: ---

There is a problem when having two different variables both of which are const
references and which have the same name but are in different anonymous
namespaces.  You'll need at least two files to reproduce this, say a.cpp and
b.cpp.

a.cpp:
namespace { const int &foo = 1; }
int main() {}

b.cpp:
namespace { const int &foo = 2; }


Then compile and link:
$ g++ -c a.cpp b.cpp
$ g++ a.o b.o
b.o:(.rodata+0x0): multiple definition of `_ZGRN12_GLOBAL__N_13fooE_'
a.o:(.rodata+0x0): first defined here
collect2: error: ld returned 1 exit status


This works fine in older versions of GCC and in clang.

Looking at the output on https://gcc.godbolt.org/z/A6LxAG shows the following
differences:
- The mangled name for "reference temporary #0 for (anonymous namespace)::foo"
changed from _ZGRN12_GLOBAL__N_13fooE0 to _ZGRN12_GLOBAL__N_13fooE_. 
Interestingly, godbolt.org is unable to demangle the second name.
- There is an additional line `.globl  _ZGRN12_GLOBAL__N_13fooE_`.  This seems
like the part causing the issue.

I originally found this when using objects of class type with user-defined
constructors.  I reduced it down to using `int` for this bug report.  Any
proposed fix should probably be tested with such types in addition to plain
`int`.


gcc version 9.1.0 (GCC)
Target: x86_64-pc-linux-gnu

Reply via email to