title: #define scope in multiple files
body: <p>I have a main file like so:</p>
  
  <p>main_a.c:</p>
  
  <pre><code>#define MAIN_A
  
  #include &lt;stdio.h&gt;
  #include "shared.h"
  
  extern int i;
  int main() {
    printf("i is: %d\n", i);
    return 0;
  }
  </code></pre>
  
  <p>I want to use the define in shared.h like this:</p>
  
  <p>shared.h</p>
  
  <pre><code>#if defined(MAIN_A)
  # define A
  #endif
  </code></pre>
  
  <p>So I can declare a variable according to whether the main file is present or not, like this:</p>
  
  <p>shared.c</p>
  
  <pre><code>#include "shared.h"
  
  #if defined(A)
  int i = 1;
  #else
  int i = 0;
  #endif
  </code></pre>
  
  <p>I build it using a makefile which looks like this:</p>
  
  <p>Makefile:</p>
  
  <pre><code>all : a
      ./a
  
  a : main_a.o shared.o
      gcc -o $@ $^
  
  %.o : %.c
      gcc -c $&lt;
  </code></pre>
  
  <p>However this prints</p>
  
  <pre><code>i is: 0
  </code></pre>
  
  <p>Now my question is: Why is it that the define seems to be lost when I compile the shared module? I know the main module is compiled first, so the define should have been resolved by the time shared.c is compiled.</p>
  
  <p>One suspicion I have is that the preprocessor might get run at the start of each module build and not just at the start of the project. If this is correct is there a way of compiling more than a single module at a time to use the preprocessor as I attempt above? </p>