Hi,
EDIT: Thanks for the answers so far, at least I can compile it now, but I still get a segmentation error.
For compilation I use the following line:
gcc -g -O0 -I../include -L../ test.c -static -lrt
Source Code is as follows:
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
struct timespec *diff(struct timespec *start, struct timespec *end);
int main()
{
struct timespec time1, time2;
int i;
int temp = 0;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (i = 0; i< 242000000; i++)
temp+=temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printf("sec: %d, nsec: %f",diff(&time1,&time2)->tv_sec, diff(&time1,&time2)->tv_nsec);
//cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;
return 0;
}
struct timespec *diff(struct timespec *start, struct timespec *end)
{
struct timespec *temp;
if ((end->tv_nsec-start->tv_nsec)<0) {
temp->tv_sec = end->tv_sec-start->tv_sec-1;
temp->tv_nsec = 1000000000+end->tv_nsec-start->tv_nsec;
} else {
temp->tv_sec = end->tv_sec-start->tv_sec;
temp->tv_nsec = end->tv_nsec-start->tv_nsec;
}
return temp;
}
I get now the following warning:
test.c: In function ‘main’: test.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘__time_t’ test.c:17: warning: format ‘%f’ expects type ‘double’, but argument 3 has type ‘long int’
Segmentation fault is surely caused by my handling of the structures. It is quite a while ago that i last had to deal with C....
Many thanks, Marcus