1

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

#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/boost-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 期间没有使用 static-libs 标志吗?使用静态库会更好吗?也许这是一个切线。还有什么可能导致上面给出的错误?请原谅我的无知,因为我对 C++/boost 还很陌生。

谢谢

4

3 回答 3

3

我没有使用cpu_timer我自己,但快速的谷歌搜索似乎表明你应该包括在内<boost/timer/timer.hpp>。至于nanosecond_type你的错误需要使用另一种using说法。

于 2012-03-18T03:03:26.967 回答
1

I think I figured out what the problem is. I was quoting an example in my original post from the ver 1.49 documentation. cpu_timer was first discussed in the boost documentation in ver 1.48. The stable version on gentoo is currently 1.46 and testing only provides ver 1.47, ver 1.48 is hardmasked. So my only option is to remove boost from my system download the tar of 1.49 and possibly break my system wrt boost or wait for the hardmask to be removed from ver 1.48.

于 2012-03-18T15:19:13.657 回答
0

无论如何,静态库肯定是无关紧要的,因为这是编译器错误,而不是链接器错误。在链接器阶段之前它不会查看库,直到那时只有标头是相关的。

于 2012-03-18T09:59:34.993 回答