Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > non static variables in a namespace (in .h) ?

Reply
Thread Tools

non static variables in a namespace (in .h) ?

 
 
vthomasset@gmail.com
Guest
Posts: n/a
 
      03-07-2007
Hi,

Sorry for the bad subject, but i couldn't figure a better one.

I would like to understand why a variable declared non static in a
header causes multiple symbol definitions (if included in different
compilation units) while the same declaration in a namespace does not.

Also, what is the correct way to declare a scoped variable in a
header ? Using extern and declaring it in a separate compilation
unit ?

Thank you,
Vincent

 
Reply With Quote
 
 
 
 
John Ratliff
Guest
Posts: n/a
 
      03-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> Sorry for the bad subject, but i couldn't figure a better one.
>
> I would like to understand why a variable declared non static in a
> header causes multiple symbol definitions (if included in different
> compilation units) while the same declaration in a namespace does not.


Could you show me an example, cause if you define a variable twice in
the same namespace, you should get multiple symbol definitions and
linker errors.

Did you mean you can define the same variable in different namespaces?
That's fine, of course.

>
> Also, what is the correct way to declare a scoped variable in a
> header ? Using extern and declaring it in a separate compilation
> unit ?


If you have to put the definition in a namespace in a header included in
multiple compilation units, then you should declare it extern and define
it in a single compilation unit.

ex:

myhdr.hh

#ifndef MYHDR_HH_
#define MYHDR_HH_

namespace myns {
extern int myvar;
}

#endif

main.cc

#include "myhdr.hh"

int myns::myvar;

int main() {
myns::myvar = 6;
}

aux.cc

#include "myhdr.hh"

void myfcn() {
myns::myvar = 10;
}

--John Ratliff
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2007
(E-Mail Removed) wrote:
> Sorry for the bad subject, but i couldn't figure a better one.
>
> I would like to understand why a variable declared non static in a
> header causes multiple symbol definitions (if included in different
> compilation units) while the same declaration in a namespace does not.


I don't believe that the second part of this statement is correct.
Are you sure about "the same declaration"?

Anyway, an object that is not const and is not explicitly declared
'static' has _external_ linkage, IOW its name is visible from other
translation units. If you define (if the declaration does not have
'extern' specifier, it's a definition) objects with the same name
in more than one translation unit, it's a violation of the ODR. You
usually get the error from the linker who is the last to check the
ODR when linking different TUs together.

If you declare the object 'static', its name is hidden from other TU
(not extern), the term is that the name has _internal_ linkage. That
way you can have objects named the same in different TUs.

> Also, what is the correct way to declare a scoped variable in a
> header ?


"Scoped"? What does that mean? Any variable is scoped, IOW every
variable has its scope.

> Using extern and declaring it in a separate compilation
> unit ?


There are variations on the theme, but they all essentially lead to
the same: declaration is available in every TU, and the definition
exist only in one TU.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2007
John Ratliff wrote:
> (E-Mail Removed) wrote:
>> Hi,
>>
>> Sorry for the bad subject, but i couldn't figure a better one.
>>
>> I would like to understand why a variable declared non static in a
>> header causes multiple symbol definitions (if included in different
>> compilation units) while the same declaration in a namespace does
>> not.

>
> Could you show me an example, cause if you define a variable twice in
> the same namespace, you should get multiple symbol definitions and
> linker errors.
>
> Did you mean you can define the same variable in different namespaces?
> That's fine, of course.


I just thought of what it could be: anonymous namespaces. But that
falls under "different" category, of course, only the OP didn't think
of it that way, most likely.

> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
vthomasset@gmail.com
Guest
Posts: n/a
 
      03-07-2007
On 7 mar, 19:49, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I would like to understand why a variable declared non static in a
> > header causes multiple symbol definitions (if included in different
> > compilation units) while the same declaration in a namespace does not.

>
> I don't believe that the second part of this statement is correct.
> Are you sure about "the same declaration"?


My example comes from some software i use. In the main include file,
it declares a
bunch of strings as in the following:

namespace x {
const std::string s1 = "txt1";
const std::string s2 = "txt2";
}

and the software compiles and runs just fine. When i saw this it
struck as being strange (that it didn't cause link time error), so i
just added a simple test string declaration outside of the namespace
and, of course, got a bunch of 'redeclared symbol' messages at link
time.

> Anyway, an object that is not const and is not explicitly declared
> 'static' has _external_ linkage, IOW its name is visible from other
> translation units. If you define (if the declaration does not have
> 'extern' specifier, it's a definition) objects with the same name
> in more than one translation unit, it's a violation of the ODR. You
> usually get the error from the linker who is the last to check the
> ODR when linking different TUs together.


These strings are const. Does it make any difference ?

> "Scoped"? What does that mean? Any variable is scoped, IOW every
> variable has its scope.


I meant in a namespace... sorry.

> There are variations on the theme, but they all essentially lead to
> the same: declaration is available in every TU, and the definition
> exist only in one TU.


OK, so being in a namespace does not change the linkage type in any
way ?

Vincent

 
Reply With Quote
 
vthomasset@gmail.com
Guest
Posts: n/a
 
      03-07-2007
On 7 mar, 19:48, John Ratliff <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> Could you show me an example, cause if you define a variable twice in
> the same namespace, you should get multiple symbol definitions and
> linker errors.


See my response to Victor Bazarow. I can give a link to the header
file on source control if you would like to see the real code, but
it's really as plain as i said (though the variables are const).

> Did you mean you can define the same variable in different namespaces?
> That's fine, of course.


No, i meant in the same namespace.

> If you have to put the definition in a namespace in a header included in
> multiple compilation units, then you should declare it extern and define
> it in a single compilation unit.


<snip example code>

OK, this is how i understand it, so i don't understand why it works
correctly in this other software.

Vincent

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2007
(E-Mail Removed) wrote:
> On 7 mar, 19:49, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> I would like to understand why a variable declared non static in a
>>> header causes multiple symbol definitions (if included in different
>>> compilation units) while the same declaration in a namespace does
>>> not.

>>
>> I don't believe that the second part of this statement is correct.
>> Are you sure about "the same declaration"?

>
> My example comes from some software i use. In the main include file,
> it declares a
> bunch of strings as in the following:
>
> namespace x {
> const std::string s1 = "txt1";
> const std::string s2 = "txt2";
> }


So, have you tried doing the same without the namespace? Does it
make an iota of difference (except for the s1 and s2 name lookup)?

> and the software compiles and runs just fine. When i saw this it
> struck as being strange (that it didn't cause link time error), so i
> just added a simple test string declaration outside of the namespace
> and, of course, got a bunch of 'redeclared symbol' messages at link
> time.


Nothing strange. Objects are 'const', they have internal linkage.

>
>> Anyway, an object that is not const and is not explicitly declared
>> 'static' has _external_ linkage, IOW its name is visible from other
>> translation units. If you define (if the declaration does not have
>> 'extern' specifier, it's a definition) objects with the same name
>> in more than one translation unit, it's a violation of the ODR. You
>> usually get the error from the linker who is the last to check the
>> ODR when linking different TUs together.

>
> These strings are const. Does it make any difference ?


Absodamnlutely.

>> "Scoped"? What does that mean? Any variable is scoped, IOW every
>> variable has its scope.

>
> I meant in a namespace... sorry.


Well, those that are "not in a namespace" are actually *in* the
_global_ namespace. The prefix for finding those is '::'. E.g.

int a = 0;
int main() {
double a = 3.14159;
return ::a; // note the ::
}

There are two 'a' objects here, one has global namespace scope,
the other has 'main' function scope.

>> There are variations on the theme, but they all essentially lead to
>> the same: declaration is available in every TU, and the definition
>> exist only in one TU.

>
> OK, so being in a namespace does not change the linkage type in any
> way ?


Nope.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
vthomasset@gmail.com
Guest
Posts: n/a
 
      03-07-2007
On 7 mar, 20:18, (E-Mail Removed) wrote:
> and the software compiles and runs just fine. When i saw this it
> struck as being strange (that it didn't cause link time error), so i
> just added a simple test string declaration outside of the namespace
> and, of course, got a bunch of 'redeclared symbol' messages at link
> time.


Hmmm, sorry, i just realized that i declared a non const string
outside
the namespace (which caused the error). If declared const, i don't get
a linking error, so it appears my question is actually about the const
keyword rather than on being in a namespace.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2007
(E-Mail Removed) wrote:
> On 7 mar, 20:18, (E-Mail Removed) wrote:
>> and the software compiles and runs just fine. When i saw this it
>> struck as being strange (that it didn't cause link time error), so i
>> just added a simple test string declaration outside of the namespace
>> and, of course, got a bunch of 'redeclared symbol' messages at link
>> time.

>
> Hmmm, sorry, i just realized that i declared a non const string
> outside
> the namespace (which caused the error). If declared const, i don't get
> a linking error, so it appears my question is actually about the const
> keyword rather than on being in a namespace.


Yep. 'const' without "extern" gives the object internal linkage.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
vthomasset@gmail.com
Guest
Posts: n/a
 
      03-07-2007
On 7 mar, 20:47, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > namespace x {
> > const std::string s1 = "txt1";
> > const std::string s2 = "txt2";
> > }

>
> So, have you tried doing the same without the namespace? Does it
> make an iota of difference (except for the s1 and s2 name lookup)?


I have now.

> Nothing strange. Objects are 'const', they have internal linkage.


Thanks a lot for your explanations.

Vincent


 
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
"Non-static aggregate with multiple choices has non-static otherschoice." rickman VHDL 5 03-30-2013 11:06 PM
How can I access non-static variables from static funcition? Neviton C++ 9 09-17-2007 10:21 PM
Static const variables within a namespace Sehcra C++ 3 07-22-2005 04:11 AM



Advertisments