我正在尝试使用测量函数的运行时间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, &time1);
.
.
.
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
/*code stuff*/
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
Run Code Online (Sandbox Code Playgroud)
谢谢
将这个和这个答案放在一起,我就能使它起作用。我必须添加_POSIX_C_SOURCE宏,以确保预处理器正确获取了库功能,这是通过在所有包含之前添加以下行来实现的:
#define _POSIX_C_SOURCE 199309L
Run Code Online (Sandbox Code Playgroud)
然后我开始出现unknown type name timespec错误,这是发生的原因,因为您必须明确告诉编译器timespec是struct。通过编写修复它:
struct timespec time1, time2;
Run Code Online (Sandbox Code Playgroud)
而不只是timespec time1, time2;。
| 归档时间: |
|
| 查看次数: |
10825 次 |
| 最近记录: |