Question

I have these files

consumer.cpp
consumer.hpp
defines.hpp
main.cpp
makefile
producer.cpp
producer.hpp

here's the file defines.hpp

#ifndef DEFINES_HPP
#define DEFINES_HPP

#include <cassert>
#include <pthread.h> 
#include <queue>
#include <stdlib.h>
#include <string>
#include <unistd.h>

pthread_mutex_t set_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_var = PTHREAD_COND_INITIALIZER;

std::queue<int> q;

#endif // DEFINES_HPP

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.

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<int>::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<int, std::allocator<int> >::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

Here is my makefile

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

How to solve this problem ?

Was it helpful?

Solution

In C++ (as well as in C) there is a difference between declaring and defining things like variables. What you are doing in the header file is defining the variables, which means that every source file that includes the header file will have the definitions.

In the header file you should only declare the variables, and then in a single source file define them.

So in the header file do e.g.

extern pthread_mutex_t set_queue_mutex;
extern pthread_cond_t  condition_var;

extern std::queue<int> q;

And then in a single source file put the definitions (what you have now).

OTHER TIPS

Putting this here because I had a similar problem that wasn't quite answered here. Whenever I would delete the method in question, I would get undefined reference to *method-name* and whenever I would add it back in, I would get multiple definition of *method-name*. I solved my problem (after an hour!) by simply running my make clean. It turns out that my .o files were messed up, and the Makefile did not update them properly like it was supposed to with make or make run.

Not exactly your problem, but hopefully someone stumbles across this who needs it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top