提升编译错误

时间:2010-11-22 13:06:17

标签: c++ boost compilation

我正在尝试编译我的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()

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

4 个答案:

答案 0 :(得分:7)

根据这个

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"

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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