1

clock_gettime doesn't work on MacOS Sierra anymore. Pretty sure I had this compiling correctly before Xcode 8 came out. I am really kind of stuck on what to do to get it to compile correctly.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <time.h>

int main(){
    struct timespec time1, time2;

    clock_gettime(CLOCK_MONOTONIC,&time1);
    //Some code I am trying to work out performance of...
    clock_gettime(CLOCK_MONOTONIC,&time2);
    printf("Time Taken: %ld",time2.tv_nsec - time1.tv_nsec);
}

Code like this simply fails to compile. I've been told the sierra timing library has changed. I get a compiler error for CLOCK_MONOTONIC not being defined and a warning for implicit declaration of clock_gettime, which turns into an error if I define CLOCK_MONOTONIC to something random, as it then just errors out during the linking stage.

Does anyone know of a fix or workaround to get the code compiling and executing?

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
JKent
  • 23
  • 5

1 Answers1

2

I don't think CLOCK_MONOTONIC has been around in recent times.

I believe what you want is probably mach_absolute_time() plus a conversion, as documented here; this appears to be monotonic, and absolute (which are two different things as you probably know).

Other useful hints are to be found at the following related (but I think not duplicate) questions:

Community
  • 1
  • 1
abligh
  • 23,144
  • 3
  • 41
  • 81