Reputation: 1312
I'm implementing a simple threaded application in which I have a server thread, and a gui thread. So, the thing goes a bit like this:
int main(int argc, char *argv[]) {
appMain app(argc, argv);
return app.exec();
}
appMain::appMain(int c, char **v) : argc(c), argv(v) {
this->Configuration();
}
int appMain::exec() {
appServer Server;
try {
ServerThread = boost::thread(Server);
ServerThread.join();
} catch (...) {
return EXIT_FAILURE;
}
return 0;
}
appMain::Configuration
method just picks up a configuration file and loads it into a struct appConfig
. The thing is, I need this structure to be modifiable from any place it might be used, which means I've got to use a mutex so as to avoid memory corruption.
Hoping to avoid any possible pointer problems and thread argument passing (which seems kind of painful), I decided to use a global variables, which I declared in appConfig.h:
struct appConfig config;
boost::mutex configMutex;
Thus I added my extern
declarations where I use them:
appMain.cpp
extern struct appConfig config;
extern boost::mutex configMutex;
appServer.cpp
extern struct appConfig config;
extern boost::mutex configMutex;
appServer::appServer() {
return;
}
void appServer::operator()() {
configMutex.lock();
cout << "appServer thread." << endl;
configMutex.unlock();
}
appServer::~appServer() {
return;
}
It seems to me that there shouldn't be any kind of problem at compile time, yet I get this nice gift:
appServer.o: In function `~appServer':
/usr/include/boost/exception/detail/exception_ptr.hpp:74: multiple definition of `configMutex'
appMain.o:/home/eax/CCITP/src/appMain.cpp:163: first defined here
appServer.o: In function `appServer':
/usr/include/boost/exception/exception.hpp:200: multiple definition of `config'
appMain.o:/usr/include/c++/4.6/bits/stl_construct.h:94: first defined here
collect2: ld returned 1 exit status
Any insight on how I could solve this will be appreciated...
Julian.
Upvotes: 0
Views: 486
Reputation: 56
This isn't really a boost problem per se: you've declared a global variable in a header that's therefore defined in global scope in both compilation units, leading to multiple definitions.
Declaring it extern
in the header, and defining it in exactly one .cpp file should work.
Upvotes: 4