C语言 时间规格重定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33557506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:12:15  来源:igfitidea点击:

Timespec redefinition error

cwindowsvisual-studiopthreadstimespec

提问by Vijay Manohar

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

使用 Visual Studio 2015 在 C 中执行 Pthread 程序时,出现以下错误:

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.hheader file contains the following code related to timespec:

pthread.h头文件包含有关的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 timespecstruct, 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.

我使用的其他头文件timespec都没有使用该结构,因此没有重新定义的机会。没有损坏的头文件的可能性,因为它是从 pthread 开源网站下载的。

回答by

pthreads-win32 (which I assume you're using) mayinternally include time.h(time.his also commonly included by other libraries/headers) - and time.halready declares timespec(also, it does so in a way compatible with pthreads) - yet the pthreads-win32's pthread.hdoesn'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 timespecif possible. Still, you can simply add

pthreads-win32(我假设您正在使用)可能在内部包含time.htime.h通常也被其他库/头文件包含)-并且time.h已经声明timespec(而且,它以与 pthreads 兼容的方式这样做)-但是 pthreads-win32 的pthread.h不'没有针对这种情况的有效包含守卫(对他们感到羞耻!)。pthreads 尝试声明它,因为它在内部需要它,但由于它可能不需要整个time.h,因此它会在可能的情况下尝试仅声明timespec。不过,您可以简单地添加

#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.

before #include <pthread.h>- 这将告诉 pthreads-win32 标头您已经有一个正确的timespec,并让您的代码正确编译。

Alternatively, if you're using pthreads extensively, you may wish to edit the header file itself - simply add that #define HAVE_STRUCT_TIMESPECto it somewhere near the beginning, and you're good to go.

或者,如果您广泛使用 pthreads,您可能希望编辑头文件本身 - 只需将其添加#define HAVE_STRUCT_TIMESPEC到靠近开头的某个位置,您就可以开始了。

Further reading: http://mingw-users.1079350.n2.nabble.com/mingw-error-redefinition-of-struct-timespec-td7583722.html

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