How do c++ compilers find an extern variable? : <p>I compile this program by g++ and clang++. There has a difference:<br>
g++ prints 1, but clang++ prints 2.<br>
It seems that<br>
g++: the extern varible is defined in the shortest scope.<br>
clang++: the extern varible is defined in the shortest global scope.</p>

<p>Does C++ spec has any specification about that?</p>

<p>main.cpp</p>

<pre><code>#include &lt;iostream&gt;
static int i;
static int *p = &amp;i;

int main() {
  int i;
  {
    extern int i;
    i = 1;
    *p = 2;
    std::cout &lt;&lt; i &lt;&lt; std::endl;
  }
}

</code></pre>

<p>other.cpp</p>

<pre><code>int i;
</code></pre>

<p>version: g++: 7.4.0/ clang++:10.0.0<br>
compilation: $(CXX) main.cpp other.cpp -o extern.exe</p>
