gpt4 book ai didi

g++ : error: cannot convert ‘time_t*’ to ‘void**’ for argument ‘3’ to ‘int timer_create(clockid_t, sigevent*, void**)’ 编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:09 25 4
gpt4 key购买 nike

我写了一个简单的timer函数,用g++编译,奇怪的是问timer_create的原型(prototype)是: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*)’

最佳答案

您需要一个timer_t,而不是time_t

(您收到模糊消息的原因是因为 timer_t 实际上是 void *,一旦您遵循了所有 typedef s.)

关于g++ : error: cannot convert ‘time_t*’ to ‘void**’ for argument ‘3’ to ‘int timer_create(clockid_t, sigevent*, void**)’ 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5935377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com