错误:未声明'CLOCK_MONOTONIC'(首次在此函数中使用)

error: 'CLOCK_MONOTONIC' undeclared (first use in this function)

本文关键字:函数 未声明 CLOCK 错误 MONOTONIC      更新时间:2023-10-16

我尝试在opencv 3.0中构建基于像素强度比较的对象检测(pico)代码,在构建时遇到类似错误的错误:"CLOCK_MONOTONIC"未声明(首次在此函数中使用)。谁能说出如何克服这些问题??代码如下以及我得到的错误

float getticks()
{
    struct timespec ts;
    if(clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
    {
        printf("clock_gettime errorn");
        return -1.0f;
    }
    return ts.tv_sec + 1e-9f*ts.tv_nsec;
}

我得到的错误为

picolrn.c:94:18: error: storage size of 'ts' isn't known
  struct timespec ts;
                  ^
picolrn.c:96:19: error: 'CLOCK_MONOTONIC' undeclared (first use in this function)
  if(clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
                   ^
picolrn.c:96:19: note: each undeclared identifier is reported only once for each function it appears in
make.exe[2]: *** [build/Debug/MinGW_1-Windows/picolrn.o] Error 1

添加命令行开关:编译时-D_POSIX_C_SOURCE=199309L

没有它,time.h 标头将无法定义它。