1

I'm trying to use the clock() function in my C++ code but it's not working. I've tried adding #include <time.h> or #include <ctime> but it hasn't helped. I'm using Atmel Studio.

#include <time.h>

int main(void)
{
        clock_t myClock = clock();
}

There shouldn't be an error, but I'm getting the following error messages:

recipe for target 'SAMS70.elf' failed
undefined reference to `_times'
ld returned 1 exit status

Does anyone have an idea why this might be happening?

  • Perhaps you need to link to a particular static library. – Peter L. Sep 18 '19 at 20:28
  • `clock()` is often implemented on top of `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)` - so search for that too. – Ted Lyngmo Sep 18 '19 at 20:31
  • @TedLyngmo most likely, OP will end with the same result. Their C++ implementation seems to do everything correctly, but underlying `_times` is just not there because MCU has no way of providing this information. – SergeyA Sep 18 '19 at 20:34
  • @SergeyA You are probably right. It's worth a try though. Doesn't take many seconds(!) :-) – Ted Lyngmo Sep 18 '19 at 20:36
  • `clock()` has hardware dependencies so you are normally required to implement or override a non-functional implementation with your own code using available timing hardware on your target. It is generally a very simple function to implement using an on-chip timer or sysclk interrupt. Related https://stackoverflow.com/questions/46335353/built-in-function-clock-not-working-in-atmel-studio-7-0. – Clifford Sep 18 '19 at 22:10

1 Answers1

3

MCU's normally do not expose processor use time. A library clock function calls the expected _times function to get the value, but the function is not defined for MCU for the reason stated above.

SergeyA
  • 61,605
  • 5
  • 78
  • 137