This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: anonymous namespaces and possible linker trouble
- From: Gianni Mariani <gmariani at chaincast dot com>
- To: gmariani at chaincast dot com
- Cc: gcc at gcc dot gnu dot org
- Date: Mon, 06 Jan 2003 22:53:17 -0800
- Subject: Re: anonymous namespaces and possible linker trouble
- Organization: Chaincast, Inc
- References: <3E18924B.2020100@chaincast.com>
- Reply-to: gmariani at chaincast dot com
Repost:
Can anyone comment ?
I'm basically asking what are the possibilities of conflicts of the same
symbols in different anonymous namesspaces and compilation units. This
was a problem I had seen in the past and I'd like to know wether I may
suffer this problem with gcc if I created a large number of files with
anonymous namespaces but with identical symbol names ?
G
Gianni Mariani wrote:
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