22

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.

Vijay Manohar
  • 473
  • 1
  • 7
  • 22
  • What line did that error occur on? – user3386109 Nov 06 '15 at 00:40
  • @user3386109 there is no line number mentioned ,when i click the error it is loading the following in pthreads cpp struct timespec { time_t tv_sec; long tv_nsec; }; – Vijay Manohar Nov 06 '15 at 00:42
  • Errors always have filenames and line numbers. But in any case, I'd say that either the project file is corrupted, or the system header files have been corrupted. Neither of which can be diagnosed over the internet. – user3386109 Nov 06 '15 at 00:56
  • @user3386109 I downloaded the pthread library from the following url and added to Visual studio http://web.cs.du.edu/~sturtevant/w13-sys/InstallingpthreadsforVisualStudio.pdf.Can you suggest from where to get those header files – Vijay Manohar Nov 06 '15 at 01:30
  • @MattO i have never included time.h header file in my program – Vijay Manohar Nov 06 '15 at 01:31
  • @MattO is using pragma will help to prevent the problem ?? – Vijay Manohar Nov 06 '15 at 01:36
  • 2
    @VijayManohar You do. By including stdlib.h, you include sys/types.h. Which in turn includes time.h. Your problem is essentially the same as the one linked...somewhere in your chain of included headers there are conflicting definitions of the timespec struct. – Matt O Nov 06 '15 at 01:37
  • [This may help](http://stackoverflow.com/questions/2150938/can-i-get-cs-pthread-h-to-compile-in-windows). – user3386109 Nov 06 '15 at 01:43
  • 7
    Reposting a question is frowned upon: http://stackoverflow.com/q/33114535/2336725 Work with that question, and explain why that answer didn't help. – Teepeemm Nov 06 '15 at 02:54
  • In pthread.h file ,the timespec is defined with pragma #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 */ and no other header file has been defined with "struct timespec" – Vijay Manohar Nov 06 '15 at 03:36
  • This duplicate question (and its answer) has helped ~4 times more people the the originall... Thanks! – yair Oct 21 '18 at 15:01

1 Answers1

61

pthreads-win32 (which I assume you're using) may internally include time.h (time.h is also commonly included by other libraries/headers) - and time.h already declares timespec (also, it does so in a way compatible with pthreads) - yet the pthreads-win32's pthread.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 entire time.h, it tries to declare only the timespec if possible. Still, you can simply add

#define HAVE_STRUCT_TIMESPEC

before #include <pthread.h> - that will tell the pthreads-win32 header that you already have a proper timespec, 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