title: Multiple definition of first defined here gcc
body: <p>I have these files </p>
  
  <pre><code>consumer.cpp
  consumer.hpp
  defines.hpp
  main.cpp
  makefile
  producer.cpp
  producer.hpp
  </code></pre>
  
  <p>here's the file defines.hpp</p>
  
  <pre><code>#ifndef DEFINES_HPP
  #define DEFINES_HPP
  
  #include &lt;cassert&gt;
  #include &lt;pthread.h&gt; 
  #include &lt;queue&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  
  pthread_mutex_t set_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t  condition_var = PTHREAD_COND_INITIALIZER;
  
  std::queue&lt;int&gt; q;
  
  #endif // DEFINES_HPP
  </code></pre>
  
  <p>This defines.hpp file is included in the producer.hpp and consumer.hpp. producer.hpp and consumer.hpp files respectively included to producer.cpp and consumer.cpp and yet to main.cpp.
  When compiling I get an error.</p>
  
  <pre><code>g++ -o main producer.cpp consumer.cpp main.cpp producer.hpp consumer.hpp defines.hpp -lpthread -ggdb
  /tmp/ccctuRp7.o: In function `__gnu_cxx::new_allocator&lt;int&gt;::destroy(int*)':
  /home/vardan/test/consumer.cpp:8: multiple definition of `set_queue_mutex'
  /tmp/cciGccft.o:/home/vardan/test/producer.cpp:8: first defined here
  /tmp/ccctuRp7.o: In function `std::deque&lt;int, std::allocator&lt;int&gt; &gt;::front()':
  /home/vardan/test/consumer.cpp:12: multiple definition of `condition_var'
  /tmp/cciGccft.o:/home/vardan/test/producer.cpp:11: first defined here
  /tmp/ccctuRp7.o: In function `global::consumer::entry_point(void*)':
  /home/vardan/test/consumer.cpp:17: multiple definition of `q'
  /tmp/cciGccft.o:/home/vardan/test/producer.cpp:17: first defined here
  /tmp/ccKCxptM.o: In function `main':
  /home/vardan/test/main.cpp:8: multiple definition of `set_queue_mutex'
  /tmp/cciGccft.o:/home/vardan/test/producer.cpp:8: first defined here
  /tmp/ccKCxptM.o: In function `main':
  /home/vardan/test/main.cpp:13: multiple definition of `condition_var'
  /tmp/cciGccft.o:/home/vardan/test/producer.cpp:11: first defined here
  /tmp/ccKCxptM.o: In function `main':
  /home/vardan/test/main.cpp:25: multiple definition of `q'
  /tmp/cciGccft.o:/home/vardan/test/producer.cpp:17: first defined here
  collect2: error: ld returned 1 exit status
  make: *** [main] Error 1
  </code></pre>
  
  <p>Here is my makefile</p>
  
  <pre><code>main : producer.cpp consumer.cpp main.cpp producer.hpp consumer.hpp defines.hpp
      g++ -o $@ $^ -lpthread -ggdb
  .PHONY : clean
  clean:
      rm -rf main *.swf *.o
  </code></pre>
  
  <p>How to solve this problem ?</p>