c - 如何解决这个语义错误?

标签 c linux eclipse gcc compiler-construction

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>

void my_handler ( timer_t timerid)
{
    printf("\nHello World\n");
}

int main()
{

 // declare some variables

 timer_t timerid;
 struct itimerspec itime;
 struct sigevent sevp;

 // initalize the sigevent variable
 // the memset is necessary to avoid segmentation faults 
 memset (&sevp, 0, sizeof (struct sigevent));
  sevp.sigev_notify=SIGEV_THREAD;
  sevp.sigev_notify_function=(void *)my_handler;


  timer_create (CLOCK_REALTIME, &sevp, &timerid);
  // initialize the timer specification
 // currently the offset and the period is 5 seconds  // you have to adjust this to your actual needs
  itime.it_value.tv_sec = 5;
  itime.it_value.tv_nsec = 0;
  itime.it_interval.tv_sec = 5;
  itime.it_interval.tv_nsec = 0;

 // create the timer
 timer_settime (timerid, 0, &itime, NULL);

 // this loop has to be adapted, so that the program ends after some time  while(1){
 // sleep(100);

}

错误:

Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/new.d" -MT"src/new.d" -o "src/new.o"  In 'CLOCK_REALTIME' undeclared (first use in this function)  
Field 'tv_nsec' could not be resolved       Semantic Error
Field 'tv_nsec' could not be resolved       Semantic Error
Field 'tv_sec' could not be resolved        Semantic Error
Field 'tv_sec' could not be resolved        Semantic Error
storage size of 'itime' isn't known     C/C++ Problem

我正在使用带有 Eclipse IDE 的 Linux 操作系统。

最佳答案

添加#include <time.h>一开始。

关于c - 如何解决这个语义错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20990078/

相关文章:

c - 在 C 中使用宏生成带字符串的函数名称

c - 使用 AT&T 语法通过内联汇编访问数组

mysql - 重新配置mysql会删除之前的所有数据库吗?

java - 在Eclipse中设置安全策略文件

java - Tomcat7服务器错误,目录中不存在文件或不是可读目录

eclipse - 可以 move Eclipse的tmp目录吗?

php - zend_call_method_with_N_params

c - O_DIRECT 的最小写入大小

c++ - Eclipse 无法完全解析 Eigen C++ 库,但可以编译

c - 奇怪的 NOOP 约定?