John
John

Reputation: 449

gettimeofday() not declared in this scope - Cygwin

Can cygwin make use of gettimeofday()? I'm getting the error:

lab8.cpp: In function ‘int main()’:
lab8.cpp:62:30: error: ‘gettimeofday’ was not declared in this scope
     gettimeofday(&start, NULL);
                              ^
makefile:14: recipe for target 'lab8.o' failed
make: *** [lab8.o] Error 1

I definitely included <sys/time.h>. Is there a package that I have to install or does it just not work on Windows?

EDIT: Here's a simple test that yields the same error:

#include <sys/time.h>

int main() {
    struct timeval start;

    gettimeofday(&start, NULL);
}

With the error:

$ g++ -c -Wall -g -std=c++11 test.cpp -o test.o
test.cpp: In function ‘int main()’:
test.cpp:6:30: error: ‘gettimeofday’ was not declared in this scope
     gettimeofday(&start, NULL);

Upvotes: 1

Views: 14304

Answers (2)

Marcus
Marcus

Reputation: 271

Thank you John - your question game me my answer!

In case it helps anyone else searching on this error... I got the same error:

error: ‘gettimeofday’ was not declared in this scope
  gettimeofday(&lastBigTick, NULL);

This error started happening when I moved from Debian 8 (jessie) to Debian 9 (stretch) on a raspberry pi

For me, I had NOT #included "sys/time.h" I had only #included "time.h" Not sure how it compiled before, but it did, so adding

#include "sys/time.h"

did the job for me.

Note: "time.h" is different and is still needed for other time functions, so I ended up needing both:

#include "time.h"
#include "sys/time.h"

My situation was perhaps different, as adding

#define _BSD_SOURCE

made no difference in my case - ie this was not the issue for me.

Upvotes: 3

Andrew Duncan
Andrew Duncan

Reputation: 174

You need to define _BSD_SOURCE to include the declaration gettimeofday (since glibc2.2.2 according to link below)

#define _BSD_SOURCE

#include <sys/time.h>

int main() {
    struct timeval start;

    gettimeofday(&start, NULL);
}

gettimeofday() - Unix, Linux System Call

Upvotes: 7

Related Questions