title: Undefined reference to global variable during linking
body: <p>I am trying to compile a program which is divided into 3 modules, corresponding to 3 source files: <code>a.c</code>, <code>b.c</code>, and <code>z.c</code>. <code>z.c</code> contains the <code>main()</code> function, which calls functions in <code>a.c</code> and <code>b.c</code>. Furthermore, a function in <code>a.c</code> calls a function in <code>b.c</code>, and viceversa. Finally, there is a global variable <code>count</code> which is used by the three modules and is defined in a separate header file, <code>global.h</code>.</p>
  
  <p>The code of the source files is the following:</p>
  
  <p><code>a.c</code></p>
  
  <pre><code>#include "global.h"
  #include "b.h"
  #include "a.h"
  
  int functAb() {
      functB();
      functA();
      return 0;
  }
  
  int functA() {
      count++;
      printf("A:%d\n", count);
      return 0;
  }
  </code></pre>
  
  <p><code>b.c</code></p>
  
  <pre><code>#include "global.h"
  #include "a.h"
  #include "b.h"
  
  int functBa() {
      functA();
      functB();
      return 0;
  }
  
  int functB() {
      count++;
      printf("B:%d\n", count);
      return 0;
  }
  </code></pre>
  
  <p><code>z.c</code></p>
  
  <pre><code>#include "a.h"
  #include "b.h"
  #include "global.h"
  
  int main() {
      count = 0;
      functAb();
      functBa();
      return 0;
  }
  </code></pre>
  
  <p>The header files:</p>
  
  <p><code>a.h</code></p>
  
  <pre><code>#ifndef A_H
  #define A_H
  
  #include &lt;stdio.h&gt;
  
  int functA();
  int functAb();
  
  #endif
  </code></pre>
  
  <p><code>b.h</code></p>
  
  <pre><code>#ifndef B_H
  #define B_H
  
  #include &lt;stdio.h&gt;
  
  int functB();
  int functBa();
  
  #endif
  </code></pre>
  
  <p><code>global.h</code></p>
  
  <pre><code>#ifndef GLOBAL_H
  #define GLOBAL_H
  
  extern int count;
  
  #endif
  </code></pre>
  
  <p>And, finally, the <code>makefile</code> that reproduces my error:</p>
  
  <pre><code>CC = gcc
  CFLAGS = -O3 -march=native -Wall -Wno-unused-result
  
  z:  a.o b.o z.o global.h
      $(CC) -o z a.o b.o z.o $(CFLAGS)
  a.o:    a.c b.h global.h
      $(CC) -c a.c $(CFLAGS)
  b.o:    b.c a.h global.h
      $(CC) -c b.c $(CFLAGS)
  z.o:    z.c a.h global.h
      $(CC) -c z.c $(CFLAGS)
  </code></pre>
  
  <p>With this, I can compile the objects <code>a.o</code>, <code>b.o</code>, and <code>z.o</code> fine, but, when linking with <code>make z</code>, I get <code>undefined reference to 'count'</code> in all of them:</p>
  
  <pre><code>z.o: In function `main':
  z.c:(.text.startup+0x8): undefined reference to `count'
  a.o: In function `functAb':
  a.c:(.text+0xd): undefined reference to `count'
  a.c:(.text+0x22): undefined reference to `count'
  a.o: In function `functA':
  a.c:(.text+0x46): undefined reference to `count'
  a.c:(.text+0x5b): undefined reference to `count'
  b.o:b.c:(.text+0xd): more undefined references to `count' follow
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I managed to reproduce the error in my actual code in this minimal example, so I guess there is a problem in the dependencies between modules, but I can't spot it. Can anyone point me in the right direction?</p>