2015-11-06 50 views
6

虽然使用Visual Studio 2015年用C执行的Pthread程序,我得到了以下错误:TIMESPEC重新定义错误

Error C2011 'timespec': 'struct' type redefinition 

以下是我的代码:

#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); 
} 

pthread.h头文件包含以下与timepec相关的代码:

#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 */ 

No other header f我使用的是使用timespec结构,所以没有重新定义的机会。没有机会损坏头文件,因为它已从pthread开源网站下载。

+0

该错误发生在哪条线上? – user3386109

+0

@ user3386109没有提到的行号,当我点击错误时,它正在pthreads中加载以下内容:cpp struct timespec {0} {0} {0} {0} time_t tv_sec; long tv_nsec; }; –

+0

错误总是有文件名和行号。但无论如何,我会说项目文件已损坏,或系统头文件已损坏。这两者都不能通过互联网进行诊断。 – user3386109

回答

20

并行线程-win32的(我假设你正在使用)可以内部包括time.htime.h通常也被其他的库/头文件包含) - 和time.h已经声明了timespec(也,它在兼容的方式这样做与pthreads) - 但pthreads-win32的pthread.h没有这种情况下的有效包括守卫(羞辱他们!)。 pthreads会尝试声明它,因为它在内部需要它,但由于它可能不需要整个time.h,因此如果可能,它会尝试仅声明timespec。不过,你可以简单地#include <pthread.h>前加

#define HAVE_STRUCT_TIMESPEC 

- 这将告诉并行线程,Win32头,你已经有一个适当的timespec,并会让你的代码编译正确。

或者,如果您广泛使用pthreads,您可能希望编辑头文件本身 - 只需将#define HAVE_STRUCT_TIMESPEC添加到开头附近的某个位置,并且您可以轻松完成。

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