准备看一下libev这个库,由于这个库很小,所以安装来先跑一跑。
1.源码下载
直接百度libev进入官网就能下载源文件
具体的网址为:http://dist.schmorp.de/libev/
2.库的编译
解压文件,进入文件目录
编译的时候需要首先切换为管理员权限
#./configure
#make
#make install
编译好后,它是被放在了/usr/local/lib这个文件夹里面
3.动态链接库
在运行的时候需要连接动态链接库,由于Ubuntu默认的动态链接库搜寻范围没有/usr/local/lib
参考了这个帖子:https://lonesysadmin.net/2013/02/22/error-while-loading-shared-libraries-cannot-open-shared-object-file/
采用的方法是,#vim /etc/ld.so.conf
在最后另起一行,加上/usr/local/lib,保存退出即可。
然后#ldconfig,刷新。
更详细的解释可以参考我的帖子:Ubuntu的目录与第三方库
4.官方例子
代码:
#include <stdio.h>
#include <ev.h>
ev_io stdin_watcher;
ev_timer timeout_watcher;
static void
stdin_cb(EV_P_ ev_io *w, int revents)
{
puts("stdin ready");
ev_io_stop(EV_A_ w);
ev_break(EV_A_ EVBREAK_ALL);
}
static void
timeout_cb(EV_P_ ev_timer *w, int revnts)
{
puts("time out");
ev_break(EV_A_ EVBREAK_ONE);
}
int
main(void)
{
struct ev_loop *loop = EV_DEFAULT;
ev_io_init(&stdin_watcher, stdin_cb, 0, EV_READ);
ev_io_start(loop, &stdin_watcher);
ev_timer_init(&timeout_watcher, timeout_cb, 10, 0);
ev_timer_start(loop, &timeout_watcher);
ev_run(loop, 0);
return 0;
}
保存命名为server.c文件
编译:gcc server.c -lev -o server
运行:./server
结果:time out