This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
anonymous namespaces and possible linker trouble
- From: Gianni Mariani <gmariani at chaincast dot com>
- To: gcc at gcc dot gnu dot org
- Date: Sun, 05 Jan 2003 12:15:07 -0800
- Subject: anonymous namespaces and possible linker trouble
- Organization: Chaincast, Inc
- Reply-to: gmariani at chaincast dot com
This is a question of how I could get into trouble.
I've had problems in the past with anonymous namespaces and symbol
clashes in the linker. While the code below seems to link fine in the
past (I don't remember which compiler) I had linker trouble on duplicate
definitions of "int xx".
How is this fixed in gcc now ?
I notice that symbols are still global:
.LFE1:
.Lfe1:
.size _Z1bi,.Lfe1-_Z1bi
.globl _ZN16_GLOBAL__N__Z1bi2xxE
.bss
.align 4
.type _ZN16_GLOBAL__N__Z1bi2xxE,@object
.size _ZN16_GLOBAL__N__Z1bi2xxE,4
_ZN16_GLOBAL__N__Z1bi2xxE:
.zero 4
.text
.align 2
So I'm thinking that some strange heristic is used to create a symbol
name that will likely no clash - but that's not guarenteed.
Why do symbols defined in anonymous namespaces need to have global
linkage ? Why can't they be defined using local linkage ?
like:
.local xx
.comm xx,4,4
.text
G
>>>> a.cpp
void b( int a );
namespace {
int xx;
};
void a( int a )
{
b( xx );
}
>>>>>>> b.cpp
void a( int xx );
void b( int a )
{
}
namespace {
int xx;
};
int main()
{
a( xx );
return 0;
}
>>>>> Makefile
xx : a.o b.o
$(CXX) -o xx $^
clean:
rm *.o xx