2010-11-22 46 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)。