naspinski naspinski - 1 year ago 162
Linux Question

Ubuntu Linux C++ error: undefined reference to 'clock_gettime' and 'clock_settime'

I am pretty new to Ubuntu, but I can't seem to get this to work. It works fine on my school computers and I don't know what I am not doing. I have checked usr/include and time.h is there just fine. Here is the code:

#include <iostream>
#include <time.h>
using namespace std;

int main()
timespec time1, time2;
int temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
//do stuff here
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
return 0;

I am using CodeBlocks as my IDE to build and run as well. Any help would be great, thank you.

Answer Source

Add -lrt to the end of g++ command line. This links in the "Real Time" shared library.