extern const in c++
<p>I have a header file called abc.h in which i want to define a constant with an external linkage. Thus it contains the statement</p>

<p>---------------abc.h-------------------------</p>

<pre><code>extern const int ONE = 1;
</code></pre>

<p>Next, i have main.cpp, where i want to use the value of ONE. Thus i declare the ONE in main.cpp before using it as </p>

<p>---------------main.cpp---------------------</p>

<pre><code>extern const int ONE;
int main()
{
     cout&lt;&lt;ONE&lt;&lt;endl;
}
</code></pre>

<p>I get an error "Multiple definition of first". </p>

<p>My question is , how can i declare a const with external linkage, and use it subsequently in different files, such that there is ONLY one memory location for the constant as opposed to each file containing a static version of the constant.</p>

<hr>

<p>I deleted the #include "abc.h" from main.cpp and everything works. </p>

<p>g++ abc.h main.cpp -o main </p>

<p>The address of ONE is same in header and the main. So it works. </p>

<p>But i dont understand how compiler resolves definition of ONE without include statement in the main.cpp</p>

<p>It seems like g++ does some magic. Is it a bad practice, where reader of main.cpp does not know where ONE is declared as there is no include "abc.h" in main.cpp ?</p>
