1.源碼下載地址:
鏈接:https://pan.baidu.com/s/1fxotILo3oTMC65z4qFAh9Q 密碼:oc31
2.庫的編譯與安裝
解壓文件,進入文件目錄
編譯的時候需要首先切換為管理員(root)賬戶,然后執行以下命令:
./configure
make
make install
編譯好后,它是被放在了/usr/local/lib這個文件夾里面
3.設置系統 動態庫 鏈接路徑
在運行的時候需要連接動態庫,由於默認的動態庫搜尋范圍沒有/usr/local/lib
打開配置文件: /etc/ld.so.conf
在最后另起一行,加上/usr/local/lib,保存退出即可。
然后終端執行命令 ldconfig
這樣動態庫搜尋范圍就有/usr/local/lib了,程序鏈接動態庫時就能找到libev庫了
有關於共享庫PATH以及ld.so.conf的更詳細介紹請看:http://www.cnblogs.com/nanqiang/p/7521325.html
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 -o server server.c -lev
運行:./server
結果:time out