Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Will two symbols with the same name clash?

Reply
Thread Tools

Will two symbols with the same name clash?

 
 
DeMarcus
Guest
Posts: n/a
 
      04-23-2010
Hi,

Will two symbols with the same name but in different .cpp files clash?
E.g.

file1.cpp

int varA;


file2.cpp

int varA;


Will varA clash during linking? Of course I can test it myself, but is
there a possibility that they are allowed to clash but the compiler
chooses one of them?

Sometimes I've seen compiler messages like: "multiple definitions of X,
ignoring the latter".


Thanks,
Daniel
 
Reply With Quote
 
 
 
 
DeMarcus
Guest
Posts: n/a
 
      04-23-2010
DeMarcus wrote:
> Hi,
>
> Will two symbols with the same name but in different .cpp files clash?
> E.g.
>
> file1.cpp
>
> int varA;
>
>
> file2.cpp
>
> int varA;
>
>
> Will varA clash during linking? Of course I can test it myself, but is
> there a possibility that they are allowed to clash but the compiler
> chooses one of them?
>
> Sometimes I've seen compiler messages like: "multiple definitions of X,
> ignoring the latter".
>
>
> Thanks,
> Daniel


Wait, I just came to think of that I have to declare them static!
I.e.

file1.cpp

static int varA;

file2.cpp

static int varA;


It was a long time since I used that.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-23-2010
* DeMarcus:
> Hi,
>
> Will two symbols with the same name but in different .cpp files clash?


Depends what you mean by "clash".

If the symbols have extern linkage then C++ generally does not support that.
It's called the "ODR", "One Definition Rule". But a linker may not necessarily
detect the clash -- e.g. 'int a[5]' one place and 'int* a' another place.

There are some execptions, where you are allowed multiple definitions of extern
linkage things.

The most important is "inline" routines.

A similar exception is there for static lifetime data in templates.


> E.g.
>
> file1.cpp
>
> int varA;
>
>
> file2.cpp
>
> int varA;
>
>
> Will varA clash during linking?


Depends on the linker.

But the code above is not well-defined C++.


> Of course I can test it myself, but is
> there a possibility that they are allowed to clash but the compiler
> chooses one of them?


No, not with the example above.

You can do that via the exception for templates mentioned above.

E.g., in each compilation unit, ...

template< class Dummy >
struct Var_ { static int a; };

template< class Dummy >
int Var_<Dummy>::a = 0;

typedef Var_<void> Var;

.... then use 'Var::a'.


> Sometimes I've seen compiler messages like: "multiple definitions of X,
> ignoring the latter".


That sounds like you're in violation of C++ rules.


Cheers & hth.,

- Alf
 
Reply With Quote
 
DeMarcus
Guest
Posts: n/a
 
      04-23-2010
>>
>> file1.cpp
>>
>> int varA;
>>
>>
>> file2.cpp
>>
>> int varA;
>>
>>
>> Will varA clash during linking?

>
> Depends on the linker.
>
> But the code above is not well-defined C++.
>
>


If I define the variables like this

file1.cpp
static int varA;

file2.cpp
static int varA;

Am I allowed to send either varA pointer to other places in the application?
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      04-23-2010
DeMarcus <> writes:

> DeMarcus wrote:
>> Hi,
>>
>> Will two symbols with the same name but in different .cpp files clash?
>> E.g.
>>
>> file1.cpp
>>
>> int varA;
>>
>>
>> file2.cpp
>>
>> int varA;
>>
>>
>> Will varA clash during linking? Of course I can test it myself, but
>> is there a possibility that they are allowed to clash but the
>> compiler chooses one of them?
>>
>> Sometimes I've seen compiler messages like: "multiple definitions of
>> X, ignoring the latter".
>>
>>
>> Thanks,
>> Daniel

>
> Wait, I just came to think of that I have to declare them static!
> I.e.
>
> file1.cpp
>
> static int varA;
>
> file2.cpp
>
> static int varA;
>
>
> It was a long time since I used that.


This particular use of the static keyword is deprecated in C++, IIRC.
To ensure that your two variables are not visible outside the
translation unit they are required in, use un-named namespaces:

// file1.cpp

namespace {
int varA;
}


// file2.cpp
namespace {
int varA;
}

Regards

Paul Bibbings
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      04-23-2010
DeMarcus <> writes:

>
> If I define the variables like this
>
> file1.cpp
> static int varA;
>
> file2.cpp
> static int varA;
>
> Am I allowed to send either varA pointer to other places in the
> application?


Are you able to say a little more about what you are trying to achieve?
With the little information we have, it strikes me, at least, as a
little convoluted. Your question began with name clashing, then sought
a method to make your same-named variables TU-local, and now you are
asking how you can refer to these same-named variables outside of the TU
they're defined in.

Questions to be asked include, in the first instance, "Why are your
variables named the same?" "What application-wide visibility do you want
for these variables?" Then you might want to ask yourself to what
extent your answers to these questions conflict.

Regards

Paul Bibbings



 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-23-2010
* DeMarcus:
>>>
>>> file1.cpp
>>>
>>> int varA;
>>>
>>>
>>> file2.cpp
>>>
>>> int varA;
>>>
>>>
>>> Will varA clash during linking?

>>
>> Depends on the linker.
>>
>> But the code above is not well-defined C++.
>>
>>

>
> If I define the variables like this
>
> file1.cpp
> static int varA;
>
> file2.cpp
> static int varA;
>
> Am I allowed to send either varA pointer to other places in the
> application?


Yes, but keep in mind that here they're two distinct variables.


Cheers & hth.,

- Alf
 
Reply With Quote
 
DeMarcus
Guest
Posts: n/a
 
      04-23-2010
Paul Bibbings wrote:
> DeMarcus <> writes:
>
>> If I define the variables like this
>>
>> file1.cpp
>> static int varA;
>>
>> file2.cpp
>> static int varA;
>>
>> Am I allowed to send either varA pointer to other places in the
>> application?

>
> Are you able to say a little more about what you are trying to achieve?
> With the little information we have, it strikes me, at least, as a
> little convoluted. Your question began with name clashing, then sought
> a method to make your same-named variables TU-local, and now you are
> asking how you can refer to these same-named variables outside of the TU
> they're defined in.
>
> Questions to be asked include, in the first instance, "Why are your
> variables named the same?" "What application-wide visibility do you want
> for these variables?" Then you might want to ask yourself to what
> extent your answers to these questions conflict.
>


I want to achieve the following.

file_class.cpp
const MessageClass ERROR( "Could not find file" );

class FileClass
{
public:
FileClass()
{
GlobalRegister::register( &ERROR );
}

void open( string s )
{
if( /* Error when opening file */ )
std::cerr << ERROR << std::endl;
}
};

calculation_class.cpp
const MessageClass ERROR( "Could not calculate" );

class CalculationClass
{
public:
CalculationClass()
{
GlobalRegister::register( &ERROR );
}

void calculate()
{
if( /* Error during calculation */ )
std::cerr << ERROR << std::endl;
}
};


I want to allow any file be able to use the symbol ERROR but at the same
time they shall be able to register the pointer to that variable without
clashing with other variables with the same symbol name.


 
Reply With Quote
 
DeMarcus
Guest
Posts: n/a
 
      04-23-2010
Paul Bibbings wrote:
> DeMarcus <> writes:
>
>> DeMarcus wrote:
>>> Hi,
>>>
>>> Will two symbols with the same name but in different .cpp files clash?
>>> E.g.
>>>
>>> file1.cpp
>>>
>>> int varA;
>>>
>>>
>>> file2.cpp
>>>
>>> int varA;
>>>
>>>
>>> Will varA clash during linking? Of course I can test it myself, but
>>> is there a possibility that they are allowed to clash but the
>>> compiler chooses one of them?
>>>
>>> Sometimes I've seen compiler messages like: "multiple definitions of
>>> X, ignoring the latter".
>>>
>>>
>>> Thanks,
>>> Daniel

>> Wait, I just came to think of that I have to declare them static!
>> I.e.
>>
>> file1.cpp
>>
>> static int varA;
>>
>> file2.cpp
>>
>> static int varA;
>>
>>
>> It was a long time since I used that.

>
> This particular use of the static keyword is deprecated in C++, IIRC.
> To ensure that your two variables are not visible outside the
> translation unit they are required in, use un-named namespaces:
>
> // file1.cpp
>
> namespace {
> int varA;
> }
>
>
> // file2.cpp
> namespace {
> int varA;
> }
>
> Regards
>
> Paul Bibbings


Thanks!!!
 
Reply With Quote
 
DeMarcus
Guest
Posts: n/a
 
      04-23-2010
Victor Bazarov wrote:
> DeMarcus wrote:
>> Paul Bibbings wrote:
>>> DeMarcus <> writes:
>>>
>>>> If I define the variables like this
>>>>
>>>> file1.cpp
>>>> static int varA;
>>>>
>>>> file2.cpp
>>>> static int varA;
>>>>
>>>> Am I allowed to send either varA pointer to other places in the
>>>> application?
>>>
>>> Are you able to say a little more about what you are trying to achieve?
>>> With the little information we have, it strikes me, at least, as a
>>> little convoluted. Your question began with name clashing, then sought
>>> a method to make your same-named variables TU-local, and now you are
>>> asking how you can refer to these same-named variables outside of the TU
>>> they're defined in.
>>>
>>> Questions to be asked include, in the first instance, "Why are your
>>> variables named the same?" "What application-wide visibility do you want
>>> for these variables?" Then you might want to ask yourself to what
>>> extent your answers to these questions conflict.
>>>

>>
>> I want to achieve the following.
>>
>> file_class.cpp
>> const MessageClass ERROR( "Could not find file" );
>>
>> class FileClass
>> {
>> public:
>> FileClass()
>> {
>> GlobalRegister::register( &ERROR );

>
> What does that do?
>


It registers the ERROR MessageClass to a global register that keeps
track of all messages to give them correct language.

Actually the MessageClass looks more like this.

const MessageClass ERROR( "Could_not_find_file_id" );

where ERROR when used picks up correct language string.

>> }
>>
>> void open( string s )
>> {
>> if( /* Error when opening file */ )
>> std::cerr << ERROR << std::endl;
>> }
>> };
>>
>> calculation_class.cpp
>> const MessageClass ERROR( "Could not calculate" );
>>
>> class CalculationClass
>> {
>> public:
>> CalculationClass()
>> {
>> GlobalRegister::register( &ERROR );

>
> What does that do?
>
>> }
>>
>> void calculate()
>> {
>> if( /* Error during calculation */ )
>> std::cerr << ERROR << std::endl;
>> }
>> };
>>
>>
>> I want to allow any file be able to use the symbol ERROR but at the
>> same time they shall be able to register the pointer to that variable

>
> Why? What for?
>
> > without
>> clashing with other variables with the same symbol name.

>
> What does registering have to do with compilation? Are you confusing
> run-time behavior (of your system) with the behavior of the compiler
> (who determines and reports "name clashes")?
>


I want to be able to use the same symbols (here symbol ERROR) in
different places. The symbols then may clash in link-time (unless I put
them in different namespaces as suggested), however, each and every
MessageClass are registered in a global register where the language can
be changed.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
prevent to open the SAME APPLICATION (with the same session) in two or more browser windows. jaibux@gmail.com ASP .Net 8 02-13-2007 03:24 PM
Two processes writing to the same XML file at the same time? darrel ASP .Net 2 04-05-2006 05:30 PM
Prevent two users from accessing the same file at the same time Shawn ASP .Net 2 02-19-2006 03:11 AM
Two account msgs.(the same domain) to be delivered to the same Inbox ZaMir Computer Support 3 01-30-2005 06:02 PM
Two PIX on same subnet with same gateway? This Old Man Cisco 4 10-20-2003 07:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57