g++ 中的编译错误: 错误: 无法转换 ‘time_t*’至“作废**”对于参数“3”;到“int timer_create(clockid_t, sigevent*, void**)”

发布于 2024-11-05 17:55:56 字数 2576 浏览 1 评论 0原文

我写了一个简单的定时器函数并用g++编译,奇怪的是它要求timer_create的原型是:int timer_create(clockid_t, sigevent*, void**),这是不正确的。

我的代码如下所示。

请帮我。

提前致谢。

enter code here
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

static void sig_handlerTimer1(int,siginfo_t*,void*);
time_t timerid;
int main()
{
     int i;
static struct sigaction sa;


static struct sigevent sevp;  // argument to timer_create
static struct itimerspec its; // argument to timer_gettime

memset (&sevp, 0, sizeof (struct sigevent));
sevp.sigev_value.sival_ptr = &timerid;
sevp.sigev_notify = SIGEV_SIGNAL;
sevp.sigev_notify_attributes = NULL;
sevp.sigev_signo = SIGUSR1;

/* Setting timer interval */
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;

/* Setting timer expiration */
its.it_value.tv_sec = 2;  // First expiry after 1 sec
its.it_value.tv_nsec = 0;

/* Setting the signal handlers before invoking timer*/
sa.sa_sigaction = sig_handlerTimer1;
sa.sa_flags = 0;
sigaction(SIGUSR1, &sa, NULL);

if (timer_create(CLOCK_REALTIME, &sevp, &timerid) == -1)
{
    fprintf(stderr, "LeakTracer (timer_trackStartTime): timer_create failed to create timer. " \
        "Leak measurement will be for entire duration of the execution period:%s \n", strerror(errno));
    return 0;

}

if (timer_settime(timerid, 0, &its, NULL) == -1)
{
    fprintf(stderr, "LeakTracer (timer_trackStartTime): timer_settime failed to set the timer. " \
        "Leak measurement will be for entire duration of execution period:%s \n", strerror(errno));
    return 0;

}

   for(i=0; i<10; i++)
   {
     printf("%d\n",i);
     sleep(1);
   }
  }

  void sig_handlerTimer1(int signum,siginfo_t* si, void* au)
  {

   int flag = 1;
   printf("Caught signal: %d\n",signum);
   if (timer_delete(&timerid) < 0)
   {
            fprintf(stderr, "timer deletion failed. " \
        "This may result in some memory leaks (sig_handlerTimer1):%s \n", strerror(errno));
   }
  }

错误:

enter code here
#  g++ signalTimer.cc -lrt

signalTimer.cc: In function ‘int main()’:

signalTimer.cc:40: error: cannot convert ‘time_t*’ to ‘void**’ for argument ‘3’ to ‘int timer_create(clockid_t, sigevent*, void**)’

signalTimer.cc:48: error: invalid conversion from ‘time_t’ to ‘void*’

signalTimer.cc:48: error:   initializing argument 1 of ‘int timer_settime(void*, int, const itimerspec*, itimerspec*)’

I have written a simple timer function and compiled in g++, and strangely it is asking that the prototype of timer_create is: int timer_create(clockid_t, sigevent*, void**), which is not true.

My code is shown below.

Please help me.

Thanks in advance.

enter code here
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

static void sig_handlerTimer1(int,siginfo_t*,void*);
time_t timerid;
int main()
{
     int i;
static struct sigaction sa;


static struct sigevent sevp;  // argument to timer_create
static struct itimerspec its; // argument to timer_gettime

memset (&sevp, 0, sizeof (struct sigevent));
sevp.sigev_value.sival_ptr = &timerid;
sevp.sigev_notify = SIGEV_SIGNAL;
sevp.sigev_notify_attributes = NULL;
sevp.sigev_signo = SIGUSR1;

/* Setting timer interval */
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;

/* Setting timer expiration */
its.it_value.tv_sec = 2;  // First expiry after 1 sec
its.it_value.tv_nsec = 0;

/* Setting the signal handlers before invoking timer*/
sa.sa_sigaction = sig_handlerTimer1;
sa.sa_flags = 0;
sigaction(SIGUSR1, &sa, NULL);

if (timer_create(CLOCK_REALTIME, &sevp, &timerid) == -1)
{
    fprintf(stderr, "LeakTracer (timer_trackStartTime): timer_create failed to create timer. " \
        "Leak measurement will be for entire duration of the execution period:%s \n", strerror(errno));
    return 0;

}

if (timer_settime(timerid, 0, &its, NULL) == -1)
{
    fprintf(stderr, "LeakTracer (timer_trackStartTime): timer_settime failed to set the timer. " \
        "Leak measurement will be for entire duration of execution period:%s \n", strerror(errno));
    return 0;

}

   for(i=0; i<10; i++)
   {
     printf("%d\n",i);
     sleep(1);
   }
  }

  void sig_handlerTimer1(int signum,siginfo_t* si, void* au)
  {

   int flag = 1;
   printf("Caught signal: %d\n",signum);
   if (timer_delete(&timerid) < 0)
   {
            fprintf(stderr, "timer deletion failed. " \
        "This may result in some memory leaks (sig_handlerTimer1):%s \n", strerror(errno));
   }
  }

Errors:

enter code here
#  g++ signalTimer.cc -lrt

signalTimer.cc: In function ‘int main()’:

signalTimer.cc:40: error: cannot convert ‘time_t*’ to ‘void**’ for argument ‘3’ to ‘int timer_create(clockid_t, sigevent*, void**)’

signalTimer.cc:48: error: invalid conversion from ‘time_t’ to ‘void*’

signalTimer.cc:48: error:   initializing argument 1 of ‘int timer_settime(void*, int, const itimerspec*, itimerspec*)’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-11-12 17:55:56

您需要一个 timer_t,而不是 time_t

(您收到晦涩消息的原因是,一旦您遵循了所有 typedeftimer_t 实际上是一个 void *。)

You need a timer_t, not a time_t.

(The reason you get the obscure message is because a timer_t is actually a void *, once you've followed all the typedefs.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文