title: extern keyword behavior with Visual Studio
body: <p>I had created this minimal example to illustrate my problem with extern declaration with Visual Studio 2008 (required to compile a python 2.7 extension). The same example is working well with gcc.
  The result is that I have 2 separate global_var variables instead of a unique one in lib.c</p>
  
  <p>the library interface : lib.h</p>
  
  <pre><code>#ifndef LIB_H
  #define LIB_H 1
  
  int __declspec( dllexport ) displayGlob();
  
  #endif // LIB_H
  </code></pre>
  
  <p>the library code</p>
  
  <pre><code>// lib.c
  #include&lt;stdio.h&gt;
  
  int global_var=2;
  
  int __declspec( dllexport ) displayGlob() {
      printf("lib.c global_var=%d\n", global_var);
      return global_var;
  }
  </code></pre>
  
  <p>the user code that uses the library variable "global_var" and function displayGlob()</p>
  
  <pre><code>// main.c
  
  #include"lib.h" 
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  extern int global_var=0; // must be initialized otherwise "error LNK2001: unresolved external symbol global_var"
  
  int main(int argc, char *argv[]) 
  {
    printf("main.c global_var=%d\n", global_var);
    displayGlob();
    global_var = 3;
    printf("main.c global_var=%d\n", global_var);
    displayGlob();
    exit(0);
  }
  </code></pre>
  
  <p>execution result is:</p>
  
  <pre><code>main.c global_var=0
  lib.c global_var=2
  main.c global_var=3
  lib.c global_var=2
  </code></pre>
  
  <p>Questions:</p>
  
  <p>1 - Why must I initialize the extern variable in lib.h with Visual Studio and not Gcc ?</p>
  
  <p>2 - Why the global_var lib.c displayed by the second displayGlob() is not modified to 3 ?</p>
  
  <p>thanks for any hint !</p>
  
  <p>Laurent</p>
  
  <p>UPDATE: moved extern declaration of global_var from lib.h to main.c (even simpler example)
  and of course problems remain...</p>