This question already has an answer here:
- Timespec :struct type Error c2011 4 answers
While executing a Pthread program in C using Visual Studio 2015, I got the following error:
Error C2011 'timespec': 'struct' type redefinition
The following is my code:
#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);
}
The pthread.h
header file contains the following code related to 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 */
No other header file which I use uses the timespec
struct, so there is no chance of redefining. There is no chance of a corrupted header file because it has been downloaded from pthread opensource website.
pthreads-win32 (which I assume you're using) may internally include
time.h
(time.h
is also commonly included by other libraries/headers) - andtime.h
already declarestimespec
(also, it does so in a way compatible with pthreads) - yet the pthreads-win32'spthread.h
doesn't have the valid include guards for this case (shame on them!). pthreads tries to declare it because it needs it internally, but since it's possible it won't need the entiretime.h
, it tries to declare only thetimespec
if possible. Still, you can simply addbefore
#include <pthread.h>
- that will tell the pthreads-win32 header that you already have a propertimespec
, and will let your code compile properly.Alternatively, if you're using pthreads extensively, you may wish to edit the header file itself - simply add that
#define HAVE_STRUCT_TIMESPEC
to it somewhere near the beginning, and you're good to go.Further reading: http://mingw-users.1079350.n2.nabble.com/mingw-error-redefinition-of-struct-timespec-td7583722.html