This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: anonymous namespaces and possible linker trouble



Second repost:

So if no-one is capable/willing to help, can anyone be kind enough to point me to the source files in gcc that are involved with creating the symbol names for anonymous namespaces so I can figure it out myself. I promise I'll report what I find.

G

Gianni Mariani wrote:


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





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]