Members of anonymous namespaces should get a name that is mangled in a way so
as to make them unique. This is not the case if a global variable is defined
before the keyword "namespace" is used. In that case the first global variable
name is used for the name mangling. If I then link with "-z muldefs" I get
wrong symbol resolution.
Example:
foo1.cpp:
#include <stdio.h>
int iii;
namespace {
void f() { printf("foo1\n"); }
}
void foo1() { f(); }
foo2.cpp:
#include <stdio.h>
int iii;
namespace {
void f() { printf("foo2\n"); }
}
void foo2() { f(); }
main.cpp:
extern void foo1();
extern void foo2();
int main() {
foo1();
foo2();
return 0;
}
g++ -o foo foo1.cpp foo2.cpp main.cpp -z muldefs
./foo
generates the output
foo1
foo1
instead of
foo1
foo2
because both mangled names for the functions f are "_ZN14_GLOBAL__N_iii1fEv",
and the linker chosses one of the two for both calls.
The error vanishes if I just write
namespace {}
before the definition of iii, because then the mangled name of f changes
to "_ZN25_GLOBAL__N_foo1.cppgmEtmb1fEv" which seems the correct name to me.
--
Summary: Members of anonymous namespaces get name mangled with
first global variable instead of file name
Product: gcc
Version: 3.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: fafa at freesurf dot ch
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18077