#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define CLOCKID CLOCK_REALTIME
void timer_thread(union sigval v)
{
printf("timer_thread function! %d\n", v.sival_int);
}
int main()
{
// XXX int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid);
// clockid--值:CLOCK_REALTIME,CLOCK_MONOTONIC,CLOCK_PROCESS_CPUTIME_ID,CLOCK_THREAD_CPUTIME_ID
// evp--存放环境值的地址,结构成员说明了定时器到期的通知方式和处理方式等
// timerid--定时器标识符
timer_t timerid;
struct sigevent evp;
memset(&evp, 0, sizeof(struct sigevent)); //清零初始化
evp.sigev_value.sival_int = 111; //也是标识定时器的,这和timerid有什么区别?回调函数可以获得
evp.sigev_notify = SIGEV_THREAD; //线程通知的方式,派驻新线程
evp.sigev_notify_function = timer_thread; //线程函数地址
if (timer_create(CLOCKID, &evp, &timerid) == -1)
{
perror("fail to timer_create");
exit(-1);
}
// XXX int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value,struct itimerspec *old_value);
// timerid--定时器标识
// flags--0表示相对时间,1表示绝对时间
// new_value--定时器的新初始值和间隔,如下面的it
// old_value--取值通常为0,即第四个参数常为NULL,若不为NULL,则返回定时器的前一个值
//第一次间隔it.it_value这么长,以后每次都是it.it_interval这么长,就是说it.it_value变0的时候会装载it.it_interval的值
struct itimerspec it;
it.it_interval.tv_sec = 1;
it.it_interval.tv_nsec = 0;
it.it_value.tv_sec = 1;
it.it_value.tv_nsec = 0;
if (timer_settime(timerid, 0, &it, NULL) == -1)
{
perror("fail to timer_settime");
exit(-1);
}
pause();
return 0;
}
/*
* int timer_gettime(timer_t timerid, struct itimerspec *curr_value);
* 获取timerid指定的定时器的值,填入curr_value
*
*/
这段代码是摘自 http://blog.csdn.net/leo9150285/article/details/8271910
我编译后为什么是提示undefined reference timer_create undefined reference timer_settimer?
我知道是这个提示是库里找不到相关函数定义,但是现在我要怎么才能去找到他呢
10 个解决方案
#1
自己顶一下啊
,谁来回答啊
,谁来回答啊
#2
加 链接选项 -lrt
#3
是 timer_settime
#4
TIMER_SETTIME(2) Linux Programmer's Manual TIMER_SETTIME(2)
NAME
timer_settime, timer_gettime - arm/disarm and fetch state of POSIX per-
process timer
SYNOPSIS
#include <time.h>
int timer_settime(timer_t timerid, int flags,
const struct itimerspec *new_value,
struct itimerspec * old_value);
int timer_gettime(timer_t timerid, struct itimerspec *curr_value);
Link with -lrt.
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
timer_settime(), timer_gettime(): _POSIX_C_SOURCE >= 199309L
#5
++,man timer_create或man timer_settime
#6
谢谢啊,这个函数库是librt.so.2.xxx在/lib里貌似usr/lib里么有,呵呵谢谢!
#7
这个man工具我居然都忘了,真是该死啊,谢谢
#8
谢谢,结贴啊
#9
你之前头文件不是加了么?是怎么解决的?
#10
加了,可是忘了链接到相应的库里面,
#1
自己顶一下啊
,谁来回答啊
,谁来回答啊
#2
加 链接选项 -lrt
#3
是 timer_settime
#4
TIMER_SETTIME(2) Linux Programmer's Manual TIMER_SETTIME(2)
NAME
timer_settime, timer_gettime - arm/disarm and fetch state of POSIX per-
process timer
SYNOPSIS
#include <time.h>
int timer_settime(timer_t timerid, int flags,
const struct itimerspec *new_value,
struct itimerspec * old_value);
int timer_gettime(timer_t timerid, struct itimerspec *curr_value);
Link with -lrt.
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
timer_settime(), timer_gettime(): _POSIX_C_SOURCE >= 199309L
#5
++,man timer_create或man timer_settime
#6
谢谢啊,这个函数库是librt.so.2.xxx在/lib里貌似usr/lib里么有,呵呵谢谢!
#7
这个man工具我居然都忘了,真是该死啊,谢谢
#8
谢谢,结贴啊
#9
你之前头文件不是加了么?是怎么解决的?
#10
加了,可是忘了链接到相应的库里面,