2014-11-05 88 views
4

我试图测量使用clock_gettime()功能的运行时间获得足够的“未申报”的错误。我包括time.h,我加-lrt的Makefile文件,并添加基于Eclipse CDT正确的道路。然而,当我尝试编译我不断收到这两种错误:尝试使用clock_gettime(),但是从time.h中

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

这种情况与任何类型的CLOCK_我尝试使用。我一直在阅读大量的问题/答案和教程,但一直没能找到有帮助的东西。

我包括标头是:

#include <stdlib.h> 
#include <stdio.h> 
#include <math.h> 
#include <time.h> 

我在Ubuntu 13.10 32位和具有以下CFLAGS编制上gcc-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, &time1); 
. 
. 
. 
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1); 
/*code stuff*/ 
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2); 

感谢

+0

你包含哪些头文件? – Grantly 2014-11-05 23:30:50

+0

关于'_POSIX_C_SOURCE> = 199309L'的手册页中有一个注释,你试图编译它的是什么? – 2014-11-05 23:49:32

+0

@ DavidC.Rankin上的gcc编译具有以下标志:-g -Wall -pedantic -std = C99 – Francisca 2014-11-05 23:57:44

回答

3

thisthis答案在一起我就能够使它发挥作用。我不得不加入这一行之前,我所有的包括添加_POSIX_C_SOURCE宏,以确保预处理正确获取库功能,这是我做的:

#define _POSIX_C_SOURCE 199309L 

然后我开始一个unknown type name timespec错误,这是怎么回事因为你必须明确告诉编译器timespecstruct。通过编写:

struct timespec time1, time2; 

而不是只是timespec time1, time2;

+4

使用1993年有点逆行。你可能成功地使用#define _XOPEN_SOURCE 700来请求POSIX 2008的支持(尽管你可以使用'600'作为可能更广泛使用的先前版本2004)。你也可以用'-std = gnu99'(或'-std = gnu11')来定义它们而不用在代码的顶部写一个'#define'。至于使用'struct timespec',这是阅读函数规范的问题;这就是它所说的,所以这就是你需要使用的东西,除非你提供'typedef struct timespec timespec;'(但是请注意,你不能为'struct stat'做到这一点)。 – 2014-11-06 00:36:58

+0

谢谢你,会更新它 – Francisca 2014-11-06 01:58:10