在官網下載最新的libevent安裝包:
直接下載:
http://libevent.org/
-
在當前目錄下解壓安裝包:
tar -zxvf libevent-xxxx.tzr.gz
cd libevent-2.0.22-stable
-
配置安裝庫的目標路徑:
./configure --prefix=/usr
(可能缺少gcc , 則安裝gcc sudo apt install gcc
checking for gcc… no
checking for cc… no
checking for cl.exe… no)
配置顯示:
x@xx:~/Downloads/libevent-2.1.8-stable$ ./configure --prefix=/usr
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for a thread-safe mkdir -p… /bin/mkdir -p
checking for gawk… no
checking for mawk… mawk
checking whether make sets $(MAKE)… yes
checking whether make supports nested variables… yes
checking whether make supports nested variables… (cached) yes
checking for style of include used by make… GNU
checking for gcc… gcc
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether gcc accepts -g… yes
checking for gcc option to accept ISO C89… none needed
checking whether gcc understands -c and -o together… yes
checking dependency style of gcc… gcc3
checking how to run the C preprocessor… gcc -E
checking for grep that handles long lines and -e… /bin/grep
checking for egrep… /bin/grep -E
checking for ANSI C header files… yes
checking for sys/types.h… yes
checking for sys/stat.h… yes
checking for stdlib.h… yes
checking for string.h… yes
checking for memory.h… yes
checking for strings.h… yes
checking for inttypes.h… yes
checking for stdint.h… yes
-
編譯安裝libevent庫:
make
sudo make install
-
檢測安裝是否成功
ls -al /usr/lib | grep libevent
顯示:
x@xx:~/Downloads/libevent-2.1.8-stable$ ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 Mar 4 22:49 libevent-2.1.so.6 -> libevent-2.1.so.6.0.2
-rwxr-xr-x 1 root root 1405672 Mar 4 22:49 libevent-2.1.so.6.0.2
-rw-r--r-- 1 root root 2376080 Mar 4 22:49 libevent.a
lrwxrwxrwx 1 root root 26 Mar 4 22:49 libevent_core-2.1.so.6 -> libevent_core-2.1.so.6.0.2
-rwxr-xr-x 1 root root 916144 Mar 4 22:49 libevent_core-2.1.so.6.0.2
-rw-r--r-- 1 root root 1577598 Mar 4 22:49 libevent_core.a
-rwxr-xr-x 1 root root 973 Mar 4 22:49 libevent_core.la
lrwxrwxrwx 1 root root 26 Mar 4 22:49 libevent_core.so -> libevent_core-2.1.so.6.0.2
lrwxrwxrwx 1 root root 27 Mar 4 22:49 libevent_extra-2.1.so.6 -> libevent_extra-2.1.so.6.0.2
-rwxr-xr-x 1 root root 517360 Mar 4 22:49 libevent_extra-2.1.so.6.0.2
-rw-r--r-- 1 root root 798554 Mar 4 22:49 libevent_extra.a
-rwxr-xr-x 1 root root 980 Mar 4 22:49 libevent_extra.la
lrwxrwxrwx 1 root root 27 Mar 4 22:49 libevent_extra.so -> libevent_extra-2.1.so.6.0.2
-rwxr-xr-x 1 root root 938 Mar 4 22:49 libevent.la
lrwxrwxrwx 1 root root 30 Mar 4 22:49 libevent_pthreads-2.1.so.6 -> libevent_pthreads-2.1.so.6.0.2
-rwxr-xr-x 1 root root 26224 Mar 4 22:49 libevent_pthreads-2.1.so.6.0.2
-rw-r--r-- 1 root root 26454 Mar 4 22:49 libevent_pthreads.a
-rwxr-xr-x 1 root root 1001 Mar 4 22:49 libevent_pthreads.la
lrwxrwxrwx 1 root root 30 Mar 4 22:49 libevent_pthreads.so -> libevent_pthreads-2.1.so.6.0.2
lrwxrwxrwx 1 root root 21 Mar 4 22:49 libevent.so -> libevent-2.1.so.6.0.2
測試libevent
可以直接到/usr/local/lib文件夾查看libevent,也可以通過命令直接顯示:
ls -al /usr/local/lib | grep libevent
這裏給出一個簡單的測試用例test.cpp,代碼如下:
#include <event.h>
#include <iostream>
struct event ev;
struct timeval tv;
using namespace std;
void time_cb(int fd, short event, void *argc)
{
cout << "timer wakeup" << endl;
event_add(&ev, &tv);
}
int main()
{
struct event_base *base = event_init();
tv.tv_sec = 10;
tv.tv_usec = 0;
evtimer_set(&ev, time_cb, NULL);
event_add(&ev, &tv);
event_base_dispatch(base);
return 0;
}
然後進行編譯執行,首先進入代碼文件所在文件夾,編譯:
g++ test.cpp -o test -levent
執行:
./test
執行結果如下則運行成功。
x@ubuntu:~/c$ ./test
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup
timer wakeup