gpt4 book ai didi

c++ - gettimeofday() 未在此范围内声明 - Cygwin

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:13 28 4
gpt4 key购买 nike

cygwin 可以使用 gettimeofday() 吗?我收到错误:

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

我肯定包括了<sys/time.h> .是否有我必须安装的软件包,或者它不能在 Windows 上运行?

编辑:这是一个产生相同错误的简单测试:

#include <sys/time.h>

int main() {
struct timeval start;

gettimeofday(&start, NULL);
}

出现错误:

$ 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);

最佳答案

您需要定义 _BSD_SOURCE 以包含声明 gettimeofday(根据下面的链接从 glibc2.2.2 开始)

#define _BSD_SOURCE

#include <sys/time.h>

int main() {
struct timeval start;

gettimeofday(&start, NULL);
}

gettimeofday() - Unix, Linux System Call

关于c++ - gettimeofday() 未在此范围内声明 - Cygwin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36901803/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com