使用Boost.Thread编译C ++源文件

问题描述 投票:7回答:5

我正在尝试学习如何使用C ++ Boost.Thread库。我在我的Ubuntu 11.10系统上安装了Boost库。我正在阅读Schaling的“The Boost C ++ Libraries”一书 - 特别是第66页的示例6.1。我正在尝试编译以下代码示例:

#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();
}

但是,当我从命令行使用以下代码编译它时:

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

我得到以下输出:

/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

我不知道如何解释这个。有人可以帮忙吗?非常感谢!

c++ boost boost-thread
5个回答
11
投票

这是一个链接错误。这意味着您的代码是正确的,并且您包含正确的标头,但编译器不会链接到增强线程库。要解决这个问题,你需要像这样编译:

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

如果已将Boost线程库安装到非标准路径,则还必须将其添加到搜索路径:

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

1
投票

您需要链接到库。一些Boost库完全在头文件中实现,不需要库。但其他人,如线程,部分在头文件中实现,部分在编译库代码中实现。

我相信你需要在编译命令中添加-lboost_thread-mt


1
投票

Boost线程不是仅模板库。您需要在链接/编译时添加-lboost_thread。

boost中的大多数库都是在头文件中实现的。它们可以像你一样被包含在内。另一方面,提升线程具有这样的性质,你需要依赖于它的编译单元,只有它的功能声明在标题中随时可用。因此编译器,或更正确的链接器,负责将您的调用链接到声明的函数/类需要知道在哪里查找这些符号。通过使用-lboost_thread调用编译器,您可以告诉它链接到库(-l)boost线程。


1
投票

根据您的意见,我与您分享pocketcpp编译工具的编译字符串:

g++ -static -I"\boost" "$(FULL_CURRENT_PATH)" -L"\MinGW\lib" -lboost_thread -lboost_system -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"

祝好运,


0
投票

我在上面评论过,但想在这里分享完整的命令行。

g ++ -std = c ++ 11 thread_example.cpp -lboost_thread -lboost_system

[我使用线程example.c ++作为源文件名;请替换你自己的]

© www.soinside.com 2019 - 2024. All rights reserved.