TIMESPEC重新定义错误 [英] Timespec redefinition error

查看:2764
本文介绍了TIMESPEC重新定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Visual Studio 2015年用C执行的Pthread程序,我得到了以下错误:

While executing a Pthread program in C using Visual Studio 2015, I got the following error:

Error C2011 'timespec': 'struct' type redefinition

以下是我的code:

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

pthreads.h中头文件包含有关的timespec以下code:

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

我用没有其他头文件使用的timespec 结构,因此不存在重新定义的机会。有没有损坏的头文件的机会,因为它已经从开源pthread的网站上下载。

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的(我假设你正在使用)的可能的内部包括 time.h中 time.h中 - code>( time.h中通常也被其他的库/头文件包括在内)已经声明了的timespec (另,它这样做与pthreads的兼容的方式) - 但与pthreads-win32的的 pthreads.h中没有有效的包括对于这种情况卫士(对他们的耻辱!)。 pthreads的尝试,因为它需要在内部宣布,但因为它可能不会需要整个 time.h中,它会尝试声明仅的timespec 如果可能的话。不过,你可以简单地添加

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

的#include&LT; pthreads.h中&GT; - 这将告诉pthreads的,Win32头,你已经有一个适当的的timespec ,并让你的code编译正常。

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.

另外,如果你使用pthreads的广泛应用,你可能要编辑的头文件本身 - 只需添加的#define HAVE_STRUCT_TIMESPEC 来这附近的某处开始,而你是好去。

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.

延伸阅读:<一href=\"http://mingw-users.1079350.n2.nabble.com/mingw-error-redefinition-of-struct-timespec-td7583722.html\" rel=\"nofollow\">http://mingw-users.1079350.n2.nabble.com/mingw-error-redefinition-of-struct-timespec-td7583722.html

这篇关于TIMESPEC重新定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆