c++/10591: [pch] Members of anonymous namespaces get name mangled with header name

bangerth@dealii.org bangerth@dealii.org
Thu May 1 23:26:00 GMT 2003


>Number:         10591
>Category:       c++
>Synopsis:       [pch] Members of anonymous namespaces get name mangled with header name
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu May 01 23:26:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Wolfgang Bangerth
>Release:        unknown-1.0
>Organization:
>Environment:
present mainline (3.4)
>Description:
Members of anonymous namespaces are very much like static
variables/functions, except that they are given external
linkage but a name that is mangled in a way so as to
make them unique. This is not the case if it happens in
a header file that is then precompiled, subsequently
leading to linker errors due to duplicate symbols.

Example:

echo 'namespace { int i; }' > x.h
echo '#include "x.h"'       > a.cc
echo '#include "x.h"'       > b.cc
echo 'int main () {}'      >> b.cc

/home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.h
/home/bangerth/bin/gcc-3.4-pre/bin/c++ -c a.cc
/home/bangerth/bin/gcc-3.4-pre/bin/c++ -c b.cc
/home/bangerth/bin/gcc-3.4-pre/bin/c++ a.o b.o

b.o(.bss+0x0): multiple definition of `(anonymous namespace)::i'
a.o(.bss+0x0): first defined here
collect2: ld returned 1 exit status

The reason is the mangled name this variable is given:
g/x> nm a.o
00000000 B _ZN20_GLOBAL__N_x.huNZudc1iE
g/x> nm b.o
00000000 B _ZN20_GLOBAL__N_x.huNZudc1iE
00000000 T main

The standard says that they must be unique every time,
but obviously they are not.

This situation is a little unfortunate, since for example
boost uses such variables in anonymous namespaces in some
headers, meaning that one cannot use precompiled headers
in conjunction with boost.

W.
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list