【问题标题】:Visual Studio error with 'timespec' structure带有“timespec”结构的 Visual Studio 错误
【发布时间】:2016-01-11 21:42:16
【问题描述】:

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

错误 C2011 'timespec': 'struct' 类型重新定义

以下是我的代码:

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

【问题讨论】:

标签: c visual-studio pthreads


【解决方案1】:

删除 pthread.h 中“TIMESPEC”的所有实例(先备份。)

如果我理解正确,您可能下载了 pthreads 并尝试将其安装到您的 VS 中。

但是 pthreads.h 文件不能很好地与其他头文件中已经定义的 TIMESPEC 定义配合使用。

因此,删除 pthreads.h 文件中定义了 TIMESPEC 的部分。

【讨论】:

  • 我强烈建议不要从库中删除包含的部分头文件,这是在部署和与其他开发人员合作时提出的不兼容问题。
  • 从 pthread.h 中删除可能会导致以后编译其他程序时出现问题
【解决方案2】:

添加此编译器标志:

-DHAVE_STRUCT_TIMESPEC

【讨论】:

  • 但现在我有一个新问题,int main(){pthread_create(&threads[t], NULL, PrintHello, (void *)t);} void *PrintHello(void *threadid){printf ("Hello world");pthread_exit(NULL);} 这是一个 Void 方法,但它仍然要求我返回值
【解决方案3】:

在 Visual Studio 2015 中编译包含 MariaDB 10 头文件的程序时会发生同样的问题(在 10.1.14 中看到)。

解决方案是定义以下内容:

STRUCT_TIMESPEC_HAS_TV_SEC
STRUCT_TIMESPEC_HAS_TV_NSEC

【讨论】:

    【解决方案4】:

    尽管这个问题已经被正确回答了,但还有另一种方法可以解决这个问题。

    首先,出现问题是因为pthreads-win32 内部包含time.h,它已经声明了timespec struct

    为了避免这个错误,我们唯一应该做的是:

    #define HAVE_STRUCT_TIMESPEC
    #include <pthread.h>
    

    【讨论】:

      【解决方案5】:

      在 Visual Studio 2015 上。

      我解决了添加的问题:

      #define _TIMESPEC_DEFINED
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-08
        • 2017-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多