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

StackOverflow https://stackoverflow.com/questions/9755623

  •  24-05-2021
  •  | 
  •  

Question

I'm trying to compile this simple program to start learning how to use timers:

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

I'm using gentoo linux and issuing the following command to compile:

g++ -I /usr/include/boost-1_46 timer1.cpp -o timer

I get these errors:

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

I was reading the docs under errors and warnings but the problem I am having is that I only have two libraries:

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

Is this because I did not use the static-libs flag during compile of boost? Would I be better off using static-libs? Maybe this is a tangent. What else can cause the errors given above? Please forgive my ignorance as I am pretty new to C++/boost.

Thanks

Was it helpful?

Solution 2

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.

OTHER TIPS

I haven't been using cpu_timer my self, but a quick Google search seems to indicate you should include <boost/timer/timer.hpp> instead. As for the error of nanosecond_type you need to use another using statement for that.

In any case, static-libs is certainly irrelevant because this is a compiler error, not a linker error. It doesn't look at the libraries until the linker stage, until then only the headers are relevant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top