2010-11-22 84 views
1

我试图编译我的提振简单的代码:使用G ++使用此命令加速编译错误

g++ main.cpp -o main 

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

,但我得到这样的错误:

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() 

怎么了,怎么编译它?

+0

是否安装了libboost-date-time-dev? – 0xAX 2010-11-22 13:08:09

+0

如何查看...?对不起我的愚蠢的问题,但我不熟悉linux ... – flyjohny 2010-11-22 13:14:50

回答

0

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

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

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" 
+0

这很酷,但现在它给“错误:'boost :: this_thread'尚未宣布”... – flyjohny 2010-11-22 13:22:10

+0

我给你链接这个文档给出了你正在使用的任何东西的头文件。如果你在里面搜索,你会得到答案。 – 2010-11-22 14:15:05

+0

根据这个http://www.boost.org/doc/libs/1_45_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread我应该包括#include ,但是这并不能解决问题...... :( – flyjohny 2010-11-22 14:33:44

0

你需要包含头宣布了posix_time。看看增强文档,看看它是什么(你可以尝试#include "boost/date_time/posix_time/posix_time_system.hpp",但我不知道这是足够的)。

1

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