g++ 中的编译错误: 错误: 无法转换 ‘time_t*’至“作废**”对于参数“3”;到“int timer_create(clockid_t, sigevent*, void**)”
我写了一个简单的定时器函数并用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个
timer_t
,而不是time_t
。(您收到晦涩消息的原因是,一旦您遵循了所有
typedef
,timer_t
实际上是一个void *
。)You need a
timer_t
, not atime_t
.(The reason you get the obscure message is because a
timer_t
is actually avoid *
, once you've followed all thetypedef
s.)