2015-11-06 164 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