我不得不将应用程序从C++重写为C.我在Ubuntu 12.04上使用gcc和Eclipse.这样做我遇到了这个错误
../src/TTNoddy.c: In function ‘main’:
../src/TTNoddy.c:16:2: error: unknown type name ‘timespec’
Run Code Online (Sandbox Code Playgroud)
这是我的代码片段,可以重现问题
#include <time.h>
int main(void) {
timespec TS;
TS.tv_nsec = 1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在这里很困惑 - 我是一名C++程序员,从来没有在我的生活中编写过纯粹的C应用程序,但是man页面clock_gettime清楚地表明timespec我在这里包含的time.h头文件中找到了它.我错过了什么?
使用Visual Studio 2015在C中执行Pthread程序时,出现以下错误:
Error C2011 'timespec': 'struct' type redefinition
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
#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);
}
Run Code Online (Sandbox Code Playgroud)
该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 */
Run Code Online (Sandbox Code Playgroud)
我使用的其他头文件timespec都没有使用struct,因此没有机会重新定义.由于已从pthread opensource网站下载,因此不存在损坏的头文件的可能性.