title: Multiple definition of const variables at header file
body: <p>I defined some constants in <strong>flag.h</strong> so <strong>link.c</strong> and <strong>linkedlist.h</strong> can use it.
  But when I compile with:</p>
  
  <blockquote>
    <p>clang -Wall main.c link.c linkedlist.c</p>
  </blockquote>
  
  <p>I get the following</p>
  
  <blockquote>
    <p>/tmp/linkedlist-o2mcAI.o:(.rodata+0x0): multiple definition of `<em>VARIABLE_NAME</em>'</p>
    
    <p>/tmp/link-oXhyfE.o:(.rodata+0x0): first defined here</p>
  </blockquote>
  
  <p>for all variables from <strong>flag.h</strong> used in <strong>link.c</strong> and <strong>linkedlist.c</strong>, and at the end:</p>
  
  <blockquote>
    <p>clang: error: linker command failed with exit code 1 (use -v to see invocation)</p>
  </blockquote>
  
  <hr>
  
  <p>A example code of <strong>main.c</strong>, <strong>flag.h</strong>, <strong>link.h</strong>, <strong>link.c</strong>, <strong>linkedlist.h</strong> and <strong>linkedlist.c</strong>:</p>
  
  <h1><strong><em>main.c</em></strong></h1>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "linkedlist.h"
  
  int main(void) {
      LinkedList* list = LinkedList_new();
  }
  </code></pre>
  
  <h1><strong><em>flag.h</em></strong></h1>
  
  <pre><code>#ifndef FLAG_H_
  #define FLAG_H_
  
  #include &lt;limits.h&gt;
  
  #define FALSE 0
  #define TRUE 1
  
  const int OK = 1;
  const int ERROR = -1;
  const int FLAG = 0;
  
  // other constants
  
  #endif
  </code></pre>
  
  <h1><strong><em>link.h</em></strong></h1>
  
  <pre><code>#ifndef LINK_H_
  #define LINK_H_
  
  typedef struct Link {
      int value;
      struct Link* next;
  } Link;
  
  Link* Link_new(int value);
  
  int useExample(int value);
  
  // other methods
  
  #endif
  </code></pre>
  
  <h1><strong><em>link.c</em></strong></h1>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "link.h"
  #include "flag.h"
  
  Link* Link_new(int value)
  {
      Link* link = malloc(sizeof(Link));
      link-&gt;value = value;
      link-&gt;next = NULL;
      return link;
  }
  
  useExample(int value)
  {
      if (value == 0) {
          return OK; // defined in flag.h
      } else {
          return FLAG; // defined in flag.h
      }
  }
  
  // other methods
  </code></pre>
  
  <h1><strong><em>linkedlist.h</em></strong></h1>
  
  <pre><code>#ifndef LINKEDLIST_H_
  #define LINKEDLIST_H_
  
  #include "link.h"
  
  typedef struct LinkedList {
      Link* first;
      unsigned int size;
  } LinkedList;
  
  LinkedList* LinkedList_new();
  
  int anotherUseExample(int value);
  
  // other methods
  
  #endif
  </code></pre>
  
  <h1><strong><em>linkedlist.c</em></strong></h1>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "linkedlist.h"
  #include "flag.h"
  
  LinkedList* LinkedList_new() {
      LinkedList* list = malloc(sizeof(LinkedList));
      list-&gt;first = NULL;
      list-&gt;size = 0;
      return list;
  }
  
  anotherUseExample(int value)
  {
      if (value == 0) {
          return FLAG; // defined in flag.h
      } else {
          return ERROR; // defined in flag.h
      }
  }
  
  // other methods
  </code></pre>
  
  <hr>
  
  <p>So how can I use <strong>flag.h</strong> in <strong>link.c</strong> and <strong>linkedlist.c</strong> without occur multiple definition?</p>
  
  <p>And...
  The way I'm coding header files and compiling is correct?</p>
  
  <hr>
  
  <p>The complete output with <code>-v</code>:</p>
  
  <pre><code>clang version 3.3 (tags/RELEASE_33/rc3)
  Target: i386-redhat-linux-gnu
  Thread model: posix
   "/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/main-JmZTmN.o -x c main.c
  clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
  #include "..." search starts here:
  #include &lt;...&gt; search starts here:
   /usr/local/include
   /usr/bin/../lib/clang/3.3/include
   /usr/include
   /usr/lib/gcc/i686-redhat-linux/4.8.1/include
  End of search list.
   "/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name link.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/link-FtygcZ.o -x c link.c
  clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
  #include "..." search starts here:
  #include &lt;...&gt; search starts here:
   /usr/local/include
   /usr/bin/../lib/clang/3.3/include
   /usr/include
   /usr/lib/gcc/i686-redhat-linux/4.8.1/include
  End of search list.
   "/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name linkedlist.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/linkedlist-n0zF1a.o -x c linkedlist.c
  clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
  #include "..." search starts here:
  #include &lt;...&gt; search starts here:
   /usr/local/include
   /usr/bin/../lib/clang/3.3/include
   /usr/include
   /usr/lib/gcc/i686-redhat-linux/4.8.1/include
  End of search list.
   "/usr/bin/ld" --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o a.out /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crt1.o /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crti.o /usr/lib/gcc/i686-redhat-linux/4.8.1/crtbegin.o -L/usr/lib/gcc/i686-redhat-linux/4.8.1 -L/usr/lib/gcc/i686-redhat-linux/4.8.1/../../.. -L/lib -L/usr/lib /tmp/main-JmZTmN.o /tmp/link-FtygcZ.o /tmp/linkedlist-n0zF1a.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-redhat-linux/4.8.1/crtend.o /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crtn.o
  /tmp/linkedlist-n0zF1a.o:(.rodata+0x4): multiple definition of `ERROR_indexOutOfBounds'
  /tmp/link-FtygcZ.o:(.rodata+0x4): first defined here
  /tmp/linkedlist-n0zF1a.o:(.rodata+0x8): multiple definition of `ERROR_invalidArgument'
  /tmp/link-FtygcZ.o:(.rodata+0x8): first defined here
  /tmp/linkedlist-n0zF1a.o:(.rodata+0x10): multiple definition of `FLAG_notFound'
  /tmp/link-FtygcZ.o:(.rodata+0x10): first defined here
  /tmp/linkedlist-n0zF1a.o:(.rodata+0xc): multiple definition of `FLAG_undefined'
  /tmp/link-FtygcZ.o:(.rodata+0xc): first defined here
  /tmp/linkedlist-n0zF1a.o:(.rodata+0x0): multiple definition of `OK'
  /tmp/link-FtygcZ.o:(.rodata+0x0): first defined here
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>