title: Why doesn't the "extern" keyword work in the same file?
body: <p>Please see the below example code for the use of &quot;extern&quot;. When I use the extern keyword in my code, I get a compilation error. Please suggest a solution for the problem.</p>
  <pre><code>#include&lt;iostream&gt;
  
    extern int x;
    extern int y;
    extern int z;
  
  int main(){
      
      x = 10;
      y = 15;
      
      z = (x&gt;y ? x: y);
      
      std::cout&lt;&lt;z;
       
      return 0;
  }
  
  </code></pre>
  <p>Error message:</p>
  <pre><code>example8.cpp:(.rdata$.refptr.z[.refptr.z]+0x0): undefined reference to `z';
  example8.cpp:(.rdata$.refptr.y[.refptr.y]+0x0): undefined reference to `y';
  example8.cpp:(.rdata$.refptr.x[.refptr.x]+0x0): undefined reference to `x';
  F:\DEVC_workspace\collect2.exe  [Error] ld returned 1 exit status
  </code></pre>