【问题标题】:Boost compilation error提升编译错误
【发布时间】:2010-11-22 13:06:17
【问题描述】:

我正在尝试编译我的 boost 简单代码:

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

void workerFunc(const char* msg, float delay_ms)
{
boost::posix_time::milliseconds workTime(delay_ms);

std::cout << "Worker: running, message = " << msg << 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, "Hello, Boost!", 2.5e3);

std::cout << "main: waiting for thread" << std::endl;

workerThread.join();

std::cout << "main: done" << std::endl;

return 0;
}

通过此命令使用 g++

g++ main.cpp -o main

但我收到这样的错误:

main.cpp: In function `void workerFunc(const char*, float)':
main.cpp:7: error: `boost::posix_time' has not been declared
main.cpp:7: error: `milliseconds' was not declared in this scope
main.cpp:7: error: expected `;' before "workTime"
main.cpp:12: error: `boost::this_thread' has not been declared
main.cpp:12: error: `workTime' was not declared in this scope
main.cpp: In function `int main(int, char**)':
main.cpp:21: error: no matching function for call to `boost::thread::thread(void (&)(const char*, float), const char[14], double)'
/usr/include/boost/thread/thread.hpp:35: note: candidates are: boost::thread::thread(const boost::thread&)
/usr/include/boost/thread/thread.hpp:38: note:                 boost::thread::thread(const boost::function0<void, std::allocator<boost::function_base> >&)
/usr/include/boost/thread/thread.hpp:37: note:                 boost::thread::thread()

出了什么问题,我应该如何编译它...?

【问题讨论】:

  • 是否安装了 libboost-date-time-dev?
  • 如何检查...?抱歉我的愚蠢问题,但我对 linux 不太熟悉......

标签: c++ boost compilation


【解决方案1】:

据此

http://www.boost.org/doc/libs/1_45_0/doc/html/date_time/posix_time.html

你需要这个

#include "boost/date_time/posix_time/posix_time.hpp"

【讨论】:

  • 这很酷,但现在它给出了“错误:`boost::this_thread' has not been declared”...
  • 我为您提供了文档的链接,该链接提供了您正在使用的任何内容的标题。如果您在其中搜索,您将得到答案。
  • 根据这个boost.org/doc/libs/1_45_0/doc/html/thread/… 我应该包括#include 但这并不能解决问题...:(
  • 根据您的错误,它在 /usr/include/boost/thread/thread.hpp - 如果您没有 1.45.0,则安装它或使用该版本的文档你有。
  • 这就是问题所在 - 我有一个早期版本 - 1.32 - 很多东西还没有实现......谢谢你的帮助!
【解决方案2】:

我怀疑您的系统中安装了旧版本的 Boost。阅读文件/usr/include/boost/version.hpp。根据您拥有的版本,请查阅特定于版本的文档(请参阅Boost Documentation)。或者通过使用系统的打包功能(如果可用)或手动按照安装说明安装最新版本的 Boost(请参阅Getting Started on Unix Variants)。

【讨论】:

    【解决方案3】:

    由于编译器无法识别某些类型,这意味着您缺少一些包含。

    对于 posix_time,您需要在代码顶部添加 #include "boost/date_time/posix_time/posix_time.hpp"

    【讨论】:

      【解决方案4】:

      您需要包含声明 posix_time 的标头。查看 boost 文档以了解它是什么(您可以尝试 #include "boost/date_time/posix_time/posix_time_system.hpp",但我不确定这是否足够)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-21
        • 1970-01-01
        • 2012-09-23
        • 1970-01-01
        • 2014-03-07
        • 2016-12-31
        • 2010-10-11
        相关资源
        最近更新 更多