pthread库"timespec"结构体重定义解决

摘要

使用pthreads-w32库时,提示“timespec”:“struct”类型重定义的错误,添加预编译关键字HAVE_STRUCT_TIMESPEC解决问题。

问题

图像处理过程中使用pthreads-w32多线程库(下载),使用Visual Studio 2017编译时报错:

***pthreads-w32-2-9-1-releasePre-built.2includepthread.h(320): error C2011: “timespec”:“struct”类型重定义
***ucrt	ime.h(39): note: 参见“timespec”的声明

打开两个重定义对应的文件,看到

// pthread.h
#if !defined(HAVE_STRUCT_TIMESPEC)
#define HAVE_STRUCT_TIMESPEC
#if !defined(_TIMESPEC_DEFINED)
#define _TIMESPEC_DEFINED
struct timespec {
        time_t tv_sec;
        long tv_nsec;
};
#endif /* _TIMESPEC_DEFINED */
#endif /* HAVE_STRUCT_TIMESPEC */

// ucrt	ime.h
#ifndef _CRT_NO_TIME_T
    struct timespec
    {
        time_t tv_sec;  // Seconds - >= 0
        long   tv_nsec; // Nanoseconds - [0, 999999999]
    };
#endif

于是添加预处理器定义:HAVE_STRUCT_TIMESPEC,重新编译,问题解决。
添加路径:"项目属性页"->"C/C++"->"预处理器"->"预处理器定义"
或者利用cmake生成工程时,直接添加定义即可:
add_definitions("-DHAVE_STRUCT_TIMESPEC")

pthread-w32下载地址

https://sourceforge.net/projects/pthreads4w/files/
ftp://sourceware.org/pub/pthreads-win32/dll-latest

原文地址:https://www.cnblogs.com/xikeguanyu/p/14019420.html