Reputation: 505
I know that in order to use the clock_gettime(2)
function, you have to include -lrt
in the makefile but I have no idea where it goes. Where would I put it in the example makefile.
CFLAGS = -g -Wall -std=c99
CC = gcc
objects = example.o
example: $(objects)
$(CC) $(CFLAGS) -o example $(objects)
example.o: example.c
$(CC) $(CFLAGS) -c example.c
clean:
rm test $(objects)
Edit: how my lrt looks.
What my code is:
#include "stdio.h"
#include "stdlib.h"
#include <time.h>
int main(int argc, char *argv[]) {
struct timespec starttime, endtime;
double elapsed;
clock_gettime(CLOCK_REALTIME, &starttime);
/// work to be timed
clock_gettime(CLOCK_REALTIME, &endtime);
elapsed = ((endtime.tv_sec-starttime.tv_sec)*1000000000.0 + (endtime.tv_nsec - starttime.tv_nsec))/1000000000;
// elapsed time can also be calculated as
if (endtime.tv_nsec < starttime.tv_nsec) {
// borrow a second
elapsed = (endtime.tv_sec - starttime.tv_sec - 1) + (1000000000.0 + endtime.tv_nsec - starttime.tv_nsec)/1000000000;
}
else {
elapsed = (endtime.tv_sec - starttime.tv_sec ) + (endtime.tv_nsec - starttime.tv_nsec)/1000000000;
}
}
Upvotes: 1
Views: 375
Reputation: 21223
The other answers are correct, but your problem seems to be that you're not including the necessary header files.
Add this in your source code:
#include <time.h>
Upvotes: 0
Reputation: 1847
CFLAGS = -g -Wall -std=c99
CC = gcc
LDFLAGS = -lrt
objects = example.o
example: $(objects)
$(CC) $(CFLAGS) -o example $(objects) $(LDFLAGS)
example.o: example.c
$(CC) $(CFLAGS) -c example.c
clean:
rm test $(objects)
As this is linker option, so its best to use as LDFLAGS variable which is common practice in Makefiles.
Upvotes: 0
Reputation: 224522
You want to put it on the line that links the executable. Namely, the line that specifies the -o
option. That's where the linker phase is carried out.
example: $(objects)
$(CC) $(CFLAGS) -o example $(objects) -lrt
Upvotes: 3