我安装了boost库。没有多线程,一切都会好起来的。 我该如何编译这个测试程序?
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
void hello_world() {
cout << "I'm new thread!" << endl;
}
int main(int argc, char* argv[]) {
boost::thread my_thread(&hello_world);
my_thread.join();
return 0;
}
我用过:g++ -I/home/user/boost/include testc.cpp
但g ++告诉我:
/tmp/ccQtBeSf.o: In function `main':
testc.cpp:(.text+0x5a): undefined reference to `boost::thread::join()'
/tmp/ccQtBeSf.o: In function `__static_initialization_and_destruction_0(int, int)':
testc.cpp:(.text+0xd5): undefined reference to `boost::system::generic_category()'
testc.cpp:(.text+0xdf): undefined reference to `boost::system::generic_category()'
testc.cpp:(.text+0xe9): undefined reference to `boost::system::system_category()'
/tmp/ccQtBeSf.o: In function `boost::thread_exception::thread_exception(int, char const*)':
testc.cpp:(.text._ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC5EiPKc]+0x7): undefined reference to `boost::system::system_category()'
/tmp/ccQtBeSf.o: In function `boost::detail::thread_data_base::thread_data_base()':
testc.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x1a): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccQtBeSf.o: In function `boost::thread::~thread()':
testc.cpp:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0xe): undefined reference to `boost::thread::detach()'
/tmp/ccQtBeSf.o: In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)':
testc.cpp:(.text._ZN5boost6threadC2IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[_ZN5boost6threadC5IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE]+0x23): undefined reference to `boost::thread::start_thread()'
/tmp/ccQtBeSf.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
testc.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x16): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccQtBeSf.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x8): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status
答案 0 :(得分:5)
您还需要链接Boost线程库:
g++ -I/home/user/boost/include testc.cpp -L/home/user/boost/lib -lboost_thread
(假设这些库位于/home/user/boost/lib
。)