问题
I am new to boost threads and am trying to compile a simple example I found:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
I am compiling it with g++ -Wall -I $(BOOST)/include -L $(BOOST)/lib/ -lboost_system test.cpp
and get these errors:
/tmp/ccp180QW.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x160): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x16c): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x178): undefined reference to `boost::system::system_category()'
/tmp/ccp180QW.o: In function `boost::thread_exception::thread_exception(int, char const*)':
test.cpp:(.text._ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC5EiPKc]+0x14): undefined reference to `boost::system::system_category()'
/tmp/ccp180QW.o: In function `boost::detail::thread_data_base::thread_data_base()':
test.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccp180QW.o: In function `boost::this_thread::sleep(boost::posix_time::ptime const&)':
test.cpp:(.text._ZN5boost11this_thread5sleepERKNS_10posix_time5ptimeE[boost::this_thread::sleep(boost::posix_time::ptime const&)]+0x3e): undefined reference to `boost::this_thread::hiden::sleep_until(timespec const&)'
/tmp/ccp180QW.o: In function `boost::thread::start_thread()':
test.cpp:(.text._ZN5boost6thread12start_threadEv[boost::thread::start_thread()]+0x15): undefined reference to `boost::thread::start_thread_noexcept()'
/tmp/ccp180QW.o: In function `boost::thread::~thread()':
test.cpp:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0x15): undefined reference to `boost::thread::detach()'
/tmp/ccp180QW.o: In function `boost::thread::get_id() const':
test.cpp:(.text._ZNK5boost6thread6get_idEv[boost::thread::get_id() const]+0x18): undefined reference to `boost::thread::native_handle()'
/tmp/ccp180QW.o: In function `boost::thread::join()':
test.cpp:(.text._ZN5boost6thread4joinEv[boost::thread::join()]+0x6d): undefined reference to `boost::thread::join_noexcept()'
/tmp/ccp180QW.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
test.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccp180QW.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status
I am using version 1.55 of boost and 4.6.1 of gcc on an Ubuntu 11.10. What am I missing?
Thanks
回答1:
You might try re-ordering to put library names at the end.
I tried this (boost is already in my default include and linker path, so I didn't need to specify the include and linker locations, just some library names), and it worked for me:
g++ -Wall test.cpp -o test -lboost_system -lboost_thread
So for your example, you might want:
g++ -Wall -I$(BOOST)/include test.cpp -o test -L$(BOOST)/lib -lboost_system -lboost_thread
Doing it the way around that you showed originally, and I got the same error as you did.
Also, you can make it all clearer (and you'll be wanting to do this anyway as soon as you have more than one source file), by splitting out the compile and link steps, like this:
g++ -c -Wall -o test.o test.cpp
g++ test.o -o test -lboost_system -lboost_thread
The -c on the first line tells g++ to just compile the object, and not link.
来源:https://stackoverflow.com/questions/23579805/error-in-linking-boost