public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* anonymous namespaces and possible linker trouble
@ 2003-01-05 20:27 Gianni Mariani
  2003-01-07  8:38 ` Gianni Mariani
  0 siblings, 1 reply; 5+ messages in thread
From: Gianni Mariani @ 2003-01-05 20:27 UTC (permalink / raw)
  To: gcc


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-01-09  4:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-05 20:27 anonymous namespaces and possible linker trouble Gianni Mariani
2003-01-07  8:38 ` Gianni Mariani
2003-01-08  5:04   ` Gianni Mariani
2003-01-09  2:23     ` Joe Buck
2003-01-09 10:25       ` Gianni Mariani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).