我是Boost C ++库的新手,我试图运行这个利用线程的简单程序
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
void wait(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}
void threadAction()
{
int i;
for(i=0;i<5;i++)
{
wait(1);
cout << i << endl;
}
}
int main()
{
boost::thread myThread(threadAction);
myThread.join();
}
然而,当我试图编译它时,终端向我吐了回来:
Undefined symbols for architecture x86_64:
"boost::this_thread::hiden::sleep_until(timespec const&)", referenced from:
boost::this_thread::sleep(boost::posix_time::ptime const&) in simpleThreadExample-b4b0cd.o
"boost::detail::thread_data_base::~thread_data_base()", referenced from:
boost::detail::thread_data<void (*)()>::~thread_data() in simpleThreadExample-b4b0cd.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in simpleThreadExample-b4b0cd.o
boost::thread_exception::thread_exception(int, char const*) in simpleThreadExample-b4b0cd.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in simpleThreadExample-b4b0cd.o
___cxx_global_var_init1 in simpleThreadExample-b4b0cd.o
"boost::thread::join_noexcept()", referenced from:
boost::thread::join() in simpleThreadExample-b4b0cd.o
"boost::thread::native_handle()", referenced from:
boost::thread::get_id() const in simpleThreadExample-b4b0cd.o
"boost::thread::start_thread_noexcept()", referenced from:
boost::thread::start_thread() in simpleThreadExample-b4b0cd.o
"boost::thread::detach()", referenced from:
boost::thread::~thread() in simpleThreadExample-b4b0cd.o
"typeinfo for boost::detail::thread_data_base", referenced from:
typeinfo for boost::detail::thread_data<void (*)()> in simpleThreadExample-b4b0cd.o
"vtable for boost::detail::thread_data_base", referenced from:
boost::detail::thread_data_base::thread_data_base() in simpleThreadExample-b4b0cd.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不知道为什么。
如果有帮助,我在我的
中安装了库 /usr/local directory
并使用
g++ -std=c++11 -I /usr/local/boost_1_57_0 main.cpp
编译我的程序
这可能是一件非常简单而且很小的事情(比如我忘了一个头文件),我只是忽略了,但我似乎无法找到它。如果有人有任何见解,那将是辉煌的! 谢谢!
答案 0 :(得分:0)
作为快速修复,您可以明确提供所需的Boost组件。在您的情况下,请在CMakeLists.txt文件中使用以下内容:
FIND_PACKAGE(需要提升COMPONENTS线程)
答案 1 :(得分:0)
尝试使用:-lboost_thread-mt
,在我的情况下运行正常。
例如:
g++ -O3 -stdlib=libc++ -std=c++11 -lboost_thread-mt < File-name >.cpp -o < Binary-Name >