title: c++ multiple definition of variable even with extern
body: <p>I've been trying to fix my multiple definition error and most answers seem to be to use extern but even after I did that the error still persists </p>
  
  <p>main.cpp</p>
  
  <pre><code>#include "header.h"
  
  int main(){
  
  ifstream indata;
  indata.open(file.c_str());
  
  int x;
  
  while(indata){
      indata &gt;&gt; x;
      print(x);
  }
  
  return 0;
  }
  </code></pre>
  
  <p>functions.cpp</p>
  
  <pre><code>#include "header.h"
  
  void print(int x){
      cout &lt;&lt; x &lt;&lt; endl;
  }
  </code></pre>
  
  <p>header.h </p>
  
  <pre><code>#ifndef _HEADER_H
  #define _HEADER_H
  
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  
  using namespace std;
  
  extern string file="testFile.txt";
  
  void print(int x);
  
  #endif
  </code></pre>
  
  <p>Makefile</p>
  
  <pre><code>all: main
  
  main.o: main.cpp header.h
      g++ -c main.cpp
  
  functions.o: functions.cpp header.h
      g++ -c functions.cpp
  
  main: main.o functions.o
      g++ main.o functions.o -o main
  
  check: all
      ./main
  
  clean:
      rm -f *.o main
  </code></pre>
  
  <p>and the resulting error message i get is </p>
  
  <pre><code>make
  g++ -c main.cpp
  In file included from main.cpp:1:0:
  header.h:11:15: warning: ‘file’ initialized and declared ‘extern’
   extern string file="testFile.txt";
                 ^
  g++ -c functions.cpp
  In file included from functions.cpp:1:0:
  header.h:11:15: warning: ‘file’ initialized and declared ‘extern’
   extern string file="testFile.txt";
                 ^
  g++ main.o functions.o -o main
  functions.o:functions.cpp:(.bss+0x0): multiple definition of `file'
  main.o:main.cpp:(.bss+0x0): first defined here
  collect2: error: ld returned 1 exit status
  Makefile:10: recipe for target 'main' failed
  make: *** [main] Error 1
  </code></pre>
  
  <p>Can someone point me in the right direction here.</p>