Timespec重新定义错误

Vij*_*har 11 c windows pthreads visual-studio timespec

使用Visual Studio 2015在C中执行Pthread程序时,出现以下错误:

Error C2011 'timespec': 'struct' type redefinition
Run Code Online (Sandbox Code Playgroud)

以下是我的代码:

#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>


void *calculator(void *parameter);

int main(/*int *argc,char *argv[]*/)
{
    pthread_t thread_obj;
    pthread_attr_t thread_attr;
    char *First_string = "abc"/*argv[1]*/;
    pthread_attr_init(&thread_attr);
        pthread_create(&thread_obj,&thread_attr,calculator,First_string);

}
void *calculator(void *parameter)
{
    int x=atoi((char*)parameter);
    printf("x=%d", x);
}
Run Code Online (Sandbox Code Playgroud)

pthread.h头文件包含有关的timespec下面的代码:

#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 */
Run Code Online (Sandbox Code Playgroud)

我使用的其他头文件timespec都没有使用struct,因此没有机会重新定义.由于已从pthread opensource网站下载,因此不存在损坏的头文件的可能性.

vax*_*uis 43

pthreads-win32(我假设你正在使用)可能在内部包含time.h(time.h通常也包含在其他库/头文件中) - 并且time.h已经声明timespec(同样,它以与pthreads兼容的方式执行) - 但是pthreads-win32 pthread.h不会对于这种情况,没有有效的包含警卫(对他们感到羞耻!).pthreads试图声明它,因为它需要它在内部,但由于它可能不需要整个time.h,它尝试只声明timespecif if possible.不过,你可以简单地添加

#define HAVE_STRUCT_TIMESPEC
Run Code Online (Sandbox Code Playgroud)

之前#include <pthread.h>- 这将告诉pthreads-win32标头你已经有一个正确的timespec,并将让你的代码正确编译.

或者,如果您正在广泛使用pthread,您可能希望编辑头文件本身 - 只需#define HAVE_STRUCT_TIMESPEC在开头附近的某处添加它,您就可以开始了.

进一步阅读:http://mingw-users.1079350.n2.nabble.com/mingw-error-redefinition-of-struct-timespec-td7583722.html