linux c/c++ libevent 定时提醒小小例子

首先 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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值