相关疑难解决方法(0)

带-std = c99的GCC抱怨不知道struct timespec

当我尝试在Linux上编译它时gcc -std=c99,编译器抱怨不知道struct timespec.但是,如果我编译它没有-std=c99一切正常.

#include <time.h>

int main(void)
{
  struct timespec asdf;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样,有没有办法让它继续使用-std=c99

c posix c99 timespec

40
推荐指数
2
解决办法
2万
查看次数

Timespec重新定义错误

使用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网站下载,因此不存在损坏的头文件的可能性.

c windows pthreads visual-studio timespec

11
推荐指数
1
解决办法
2万
查看次数

Timespec:struct type错误c2011

使用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);
}
Run Code Online (Sandbox Code Playgroud)

c pthreads visual-studio

7
推荐指数
2
解决办法
5773
查看次数

尝试使用clock_gettime(),但是从time.h获得了大量"未声明"的错误

我正在尝试使用测量函数的运行时间clock_gettime().我包括time.h,我添加-lrt到Makefile,并在Eclipse CDT上添加了正确的路径.但是,当我尝试编译时,我会遇到这些错误:

experiments.c: In function ‘main’:
experiments.c:137:2: error: unknown type name ‘timespec’
timespec time1, time2;
^
experiments.c:139:2: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
^
experiments.c:139:16: error: ‘CLOCK_PROCESS_CPUTIME_ID’ undeclared
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
Run Code Online (Sandbox Code Playgroud)

这种情况发生在CLOCK_我试图使用的任何类型.我一直在阅读大量的问题/答案和教程,但却找不到有用的东西.

我包括的标题是:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
Run Code Online (Sandbox Code Playgroud)

我在Ubuntu 13.10 32位上gcc并使用以下内容进行编译CFLAGS:-g -Wall -pedantic -std=c99

如果我添加-D_POSIX_C_SOURCE=199309L我得到的标志error: unknown type name ‘timespec’和有关使用的警告timespec.

这是代码的一部分,以防它有帮助:

timespec time1, time2;
int temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, …
Run Code Online (Sandbox Code Playgroud)

c time libraries

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×4

pthreads ×2

timespec ×2

visual-studio ×2

c99 ×1

libraries ×1

posix ×1

time ×1

windows ×1