首先 linux上安装好libevent
编辑代码
#include<stdio.h>
#include<event.h>
struct event ev;
struct timeval tv;
void time_cb(int fd,short event,void *argc)
{
printf("timer wakeup!\n");
event_add(&ev,&tv);
}
int main()
{
struct event_base *base = event_init();
tv.tv_sec=2;
tv.tv_usec=0;
evtimer_set(&ev,time_cb,NULL);
event_add(&ev,&tv);
event_base_dispatch(base);
return 0;
}
编译运行
结果1
[jl@localhost test]$ gcc test.c -o test
/tmp/ccN1dmqa.o: In function `time_cb':
test.c:(.text+0x29): undefined reference to `event_add'
/tmp/ccN1dmqa.o: In function `main':
test.c:(.text+0x39): undefined reference to `event_init'
test.c:(.text+0x7d): undefined reference to `event_set'
test.c:(.text+0x91): undefined reference to `event_add'
test.c:(.text+0x9d): undefined reference to `event_base_dispatch'
collect2: ld 返回 1
[jl@localhost test]
结果2
[jl@localhost test]$ gcc test.c -o test -levent
[jl@localhost test]$ ./test
timer wakeup!
timer wakeup!
timer wakeup!
timer wakeup!
timer wakeup!
timer wakeup!
timer wakeup!
timer wakeup!