编译使用Boost.Thread C ++源文件 [英] Compiling C++ source file using Boost.Thread

查看:270
本文介绍了编译使用Boost.Thread C ++源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何使用C ++ Boost.Thread库。我已经安装了我的Ubuntu 11.10系统上的Boost库。我下面的书Boost C ++库的Schaling - 66我试图编译下面code例如页面上专门例如6.1:

I am trying to learn how to use the C++ Boost.Thread library. I have installed the Boost libraries on my Ubuntu 11.10 system. I am following the book "The Boost C++ Libraries" by Schaling - specifically example 6.1 on page 66. I am trying to compile the following code example:

#include <boost/thread.hpp>
#include <iostream>

void wait(int seconds)
{ 
  boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

void thread()
{
  for(int i = 0; i < 5; ++i)
  {
    wait(1);
    std::cout << i << std::endl;
  }
}

int main() 
{
  boost::thread t(thread);
  t.join();
}

然而,当我编译此从以下命令行:

However, when I compile this with the following from the command line:

$ g++ example61.cpp -o example61 -I /usr/local/include

我得到以下的输出:

I get the following output:

/tmp/cc6bVu1F.o: In function `main':
example6.cpp:(.text+0x9d): undefined reference to `boost::thread::join()'
example6.cpp:(.text+0xae): undefined reference to `boost::thread::~thread()'
example6.cpp:(.text+0xc6): undefined reference to `boost::thread::~thread()'
/tmp/cc6bVu1F.o: In function `boost::detail::thread_data_base::thread_data_base()':
example6.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cc6bVu1F.o: In function `void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)':
example6.cpp:(.text._ZN5boost11this_thread5sleepINS_10posix_time7secondsEEEvRKT_[void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)]+0x35): undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'
/tmp/cc6bVu1F.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)':
example6.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]+0x30): undefined reference to `boost::thread::start_thread()'
/tmp/cc6bVu1F.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
example6.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cc6bVu1F.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

我不知道该如何跨preT这。任何人都可以帮忙吗?太谢谢你了!

I don't know how to interpret this. Can anyone help? Thank you so much!

推荐答案

这是一个链接错误。这意味着你的code是正确的,你包括正确的头,但是编译器不会对升压线程库链接。为了解决这个问题,你需要编译如下:

That is a linking error. It means your code is correct and you include the correct headers, but the compiler doesn't link against the boost threading library. To fix this, you need to compile like this:

g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread

如果您已经安装了升压线程库到一个非标准的路径,还必须将其添加到搜索路径:

If you've installed the Boost threading library to a non-standard path, you must also add it to the search path:

g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread -L/usr/local/lib

这篇关于编译使用Boost.Thread C ++源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆