title: multiple definition error when building C++ project with makefile
body: <p>Please go through the entire question before marking it as already answered.</p>
  
  <p>I am a beginner and learning coding all by myself, this question might seem dumb to expert developers but I have nobody else to help me out but the online community. I tried searching for an answer but did not got a specific one due to which I am stuck on it.</p>
  
  <p>I am creating a simple project for learning to use make files and I keep getting multiple definitions error.</p>
  
  <p>Below is the code for <strong><code>Makefile</code></strong></p>
  
  <pre><code>all: vecy
  
  vecy: main1.o vec.o
      g++ -o vecy main1.o vec.o
  
  main1.o: main1.cpp vec.h
      g++ -c main1.cpp
  
  vec.o: vec.cpp vec.h
      g++ -c vec.cpp
  
  .PHONY: clean
  clean:
      rm -f *.o vecy
  </code></pre>
  
  <p>Below are the <strong><code>Errors</code></strong> which I get on executing the Makefile</p>
  
  <pre><code>g++ -o vecy main1.o vec.o
  vec.o:(.bss+0x0): multiple definition of `vec'
  main1.o:(.bss+0x0): first defined here
  vec.o:(.bss+0x18): multiple definition of `sum'
  main1.o:(.bss+0x18): first defined here
  vec.o:(.bss+0x20): multiple definition of `i'
  main1.o:(.bss+0x20): first defined here
  vec.o:(.bss+0x24): multiple definition of `n'
  main1.o:(.bss+0x24): first defined here
  collect2: error: ld returned 1 exit status
  Makefile:4: recipe for target 'vecy' failed
  make: *** [vecy] Error 1
  </code></pre>
  
  <p>Below is the code for <strong><code>main1.cpp</code></strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "vec.h"
  
  int main(){
  input();
  print();
  }
  </code></pre>
  
  <p>Below is the code for <strong><code>vec.h</code></strong></p>
  
  <pre><code>#include &lt;vector&gt;
  
  #ifndef vec_h
  #define vec_h
  
  std::vector&lt;int&gt; vec;
  
  long long int sum;
  
  int i, n;
  
  void input();
  void print();
  
  #endif
  </code></pre>
  
  <p>Below is the code for <strong><code>vec.cpp</code></strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "vec.h"
  
  using namespace std;
  
  void input(){
  
  cout &lt;&lt; "Enter the number of elements you want to enter into the vector = ";
  cin &gt;&gt; n;
  
  for (i = 1; i &lt;= n; i++){
  int alpha;  cin &gt;&gt; alpha;
  sum += alpha;
  vec.push_back(alpha);
  }
  }
  
  void print(){
  
  cout &lt;&lt; "elements entered into the vector are\n\n";
  
  for (i = 0; i &lt; n; i++){
      cout &lt;&lt; vec[i] &lt;&lt; "  ";
  }
  cout &lt;&lt; endl;
  cout &lt;&lt; "Sum of elements entered in the vector = " &lt;&lt; sum &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>where is the problem? I have only defined <code>vec</code> <code>sum</code> <code>i</code> and <code>n</code> once in the header file <code>vec.h</code>and it has file guards in place so must not be getting included twice.</p>
  
  <p>Would really appreciate any help on this.</p>