title: How to use "extern struct" to share variables in c programming and compile with gcc?
body: <p>I want some shared variables should be accessed among source files, <em>main.c</em> and <em>second.c</em> and  my header file is <em>all.h</em> defined the shared data type,</p>
  
  <pre><code>#ifndef ALL_H
  #define ALL_H
  struct foo {
      double v;
      int i;
  };
  
  struct bar {
      double x;
      double y;
  };
  #endif
  </code></pre>
  
  <p><em>main.c</em> is given below</p>
  
  <pre><code>/* TEST*/
  #include "all.h"
  #include "second.h"
  
  int main(int argc, char* argv[])
  {
      struct foo fo; // should be accessed in second.c
      fo.v= 1.1;
      fo.i = 12;
  
      struct bar ba; // should be accessed in second.c
      ba.x= 2.1;
      ba.y= 2.2;
  
      sec(); // function defined in second.c
  
      return 0;
  }
  </code></pre>
  
  <p><em>second.h</em> is given below</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "all.h"
  
  int sec();
  </code></pre>
  
  <p><em>second.c</em> is given below</p>
  
  <pre><code>#include "second.h"
  
  extern struct foo fo;
  extern struct bar ba;
  
  int sec()
  {
      printf("OK is %f\n", fo.v+ba.x);
  
      return 0;
  }
  </code></pre>
  
  <p>I thought i have all the declaration and include the headers. But when i compile  </p>
  
  <pre><code>    gcc -o main main.c second.c 
  
  or 
  
      gcc -c second.c
      gcc -c main.c
      gcc -o main main.o second.o
  </code></pre>
  
  <p>It will give some error like </p>
  
  <pre><code>second.o: In function `sec':
  second.c:(.text+0x8): undefined reference to `fo'
  second.c:(.text+0xe): undefined reference to `ba'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I think somewhere of the use of <code>extern</code> was wrong or i use the <code>gcc</code> incorrectly?</p>