开始使用boost-cpu_timer时,错误:“boost::timer::cpu_timer”尚未声明

getting started with boost cpu_timer, error: ‘boost::timer::cpu_timer’ has not been declared

本文关键字:timer boost cpu 未声明 boost-cpu 开始 错误      更新时间:2023-10-16

我正试图编译这个简单的程序来开始学习如何使用计时器:

#include <boost/timer.hpp>
using boost::timer::cpu_timer;
//...
nanosecond_type last_checkpoint_time = 0;
cpu_timer checkpoint_timer;  // start the timer
for (;;)
{
  //process_a_transaction();
  if (checkpoint_timer.elapsed().user - last_checkpoint_time > 5*1000000000LL)
  {
    //... create a checkpoint ...
    last_checkpoint_time = checkpoint_timer.elapsed().user;  
    cout << checkpoint_timer.elapsed().user << endl;
  }
}

我使用gentoo-linux并发出以下命令进行编译:

g++-I/usr/include/brust-1-46 timer1.cpp-o timer

我得到这些错误:

timer1.cpp:3:21: error: ‘boost::timer::cpu_timer’ has not been declared
timer1.cpp:5:1: error: ‘nanosecond_type’ does not name a type
timer1.cpp:6:1: error: ‘cpu_timer’ does not name a type
timer1.cpp:8:1: error: expected unqualified-id before ‘for’
timer1.cpp:8:8: error: expected unqualified-id before ‘)’ token

我在错误和警告下阅读文档,但我遇到的问题是我只有两个库:

/usr/lib/libboost_test_exec_monitor-1_46.a
/usr/lib/libboost_test_exec_monitor-mt-1_46.a

这是因为我在编译boost时没有使用静态libs标志吗?使用静态库会更好吗?也许这是一条切线。还有什么原因会导致上述错误?请原谅我的无知,因为我对C++/boost还很陌生。

感谢

我自己还没有使用cpu_timer,但在谷歌上快速搜索似乎表明应该包含<boost/timer/timer.hpp>。至于nanosecond_type的错误,您需要使用另一个using语句。

我想我已经找到了问题所在。我在1.49版本的文档中引用了我的原始文章中的一个例子。cputimer在1.48版本的boost文档中首次进行了讨论。gentoo上的稳定版本目前是1.46,测试只提供1.47版本,1.48版本是硬屏蔽的。因此,我唯一的选择是从我的系统中删除boost下载1.49的tar,并可能破坏我的系统wrt boost或等待从1.48版本中删除硬掩码。

在任何情况下,静态库都是不相关的,因为这是编译器错误,而不是链接器错误。直到链接器阶段,它才查看库,在此之前,只有标头是相关的。