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