前言:函數
producer使用libevent接收http post請求post
libevent下載:測試
官網:網站
版本: libevent-2.0.22-stable.tar.gz.net
有時候從該網站沒法進行下載,所以能夠經過以下連接下載:code
https://sourceforge.net/projects/levent/?source=typ_redirect事件
解壓:ssl
tar -xvf libevent-2.0.22-stable.tar.gzget
配置:
解壓後進入目錄,進行配置,把庫安裝到/home/andrew/Work/others/libevent_test目錄下
./configure --prefix=/home/andrew/Work/others/libevent_test
編譯安裝
sudo make
sudo make install
查看是否安裝成功
[root@localhost libevent_test]# ls -al /home/andrew/Work/others/libevent_test/lib |grep libevent
lrwxrwxrwx. 1 root root 21 May 4 20:48 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 971791 May 4 20:48 libevent-2.0.so.5.1.9
-rw-r--r--. 1 root root 1575592 May 4 20:48 libevent.a
lrwxrwxrwx. 1 root root 26 May 4 20:48 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 588212 May 4 20:48 libevent_core-2.0.so.5.1.9
-rw-r--r--. 1 root root 981888 May 4 20:48 libevent_core.a
-rwxr-xr-x. 1 root root 1004 May 4 20:48 libevent_core.la
lrwxrwxrwx. 1 root root 26 May 4 20:48 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx. 1 root root 27 May 4 20:48 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 404958 May 4 20:48 libevent_extra-2.0.so.5.1.9
-rw-r--r--. 1 root root 593776 May 4 20:48 libevent_extra.a
-rwxr-xr-x. 1 root root 1011 May 4 20:48 libevent_extra.la
lrwxrwxrwx. 1 root root 27 May 4 20:48 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 969 May 4 20:48 libevent.la
lrwxrwxrwx. 1 root root 29 May 4 20:48 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 94225 May 4 20:48 libevent_openssl-2.0.so.5.1.9
-rw-r--r--. 1 root root 131924 May 4 20:48 libevent_openssl.a
-rwxr-xr-x. 1 root root 1040 May 4 20:48 libevent_openssl.la
lrwxrwxrwx. 1 root root 29 May 4 20:48 libevent_openssl.so -> libevent_openssl-2.0.so.5.1.9
lrwxrwxrwx. 1 root root 30 May 4 20:48 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 18462 May 4 20:48 libevent_pthreads-2.0.so.5.1.9
-rw-r--r--. 1 root root 18702 May 4 20:48 libevent_pthreads.a
-rwxr-xr-x. 1 root root 1032 May 4 20:48 libevent_pthreads.la
lrwxrwxrwx. 1 root root 30 May 4 20:48 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx. 1 root root 21 May 4 20:48 libevent.so -> libevent-2.0.so.5.1.9
測試用例:
#include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <event.h> // 定時事件回調函數 void onTime(int sock, short event, void *arg) { printf("Hello,World!\n"); struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; // 從新添加定時事件(定時事件觸發後默認自動刪除) event_add((struct event*)arg, &tv); } int main() { // 初始化 event_init(); struct event ev_time; // 設置定時事件 evtimer_set(&ev_time, onTime, &ev_time); struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; // 添加定時事件 event_add(&ev_time, &tv); // 事件循環 event_dispatch(); return 0; }
編譯:
gcc -g -fno-inline -lrt -lpopt example1.c -o example1 -L ./lib -levent -I ./include
運行:
[root@localhost libevent_test]# ./example1
./example1: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
解決方法1:
cp /home/andrew/Work/others/libevent_test/lib/libevent-2.0.so.5 /usr/local/lib
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
解決方法2:
這個是程序找不到共享庫的位置,經過執行echo $LD_LIBRARY_PATH能夠看到系統庫的環境變量裏沒有咱們安裝的路徑,即由--prefix制定的路徑
[root@localhost libevent_test]# echo $LD_LIBRARY_PATH
/home/andrew/Work/others/libevent_test/lib:
[root@localhost libevent_test]# export LD_LIBRARY_PATH=/home/andrew/Work/others/libevent_test/lib/:$LD_LIBRARY_PATH
由於在指定路徑下libevent分別生成了靜態和動態庫,因此優先選擇動態庫。
再次運行:
[root@localhost libevent_test]# ./example1
Hello,World!
Hello,World!
Hello,World!
注意
若是隻編譯出動態庫,則編譯以下所示:
./configure --prefix=/home/andrew/Work/others/libevent_test --enable-shared=no
make
make install
[root@localhost lib]# ll
total 3256
-rw-r--r--. 1 root root 1575592 May 5 00:36 libevent.a
-rw-r--r--. 1 root root 981888 May 5 00:36 libevent_core.a
-rwxr-xr-x. 1 root root 916 May 5 00:36 libevent_core.la
-rw-r--r--. 1 root root 593776 May 5 00:36 libevent_extra.a
-rwxr-xr-x. 1 root root 919 May 5 00:36 libevent_extra.la
-rwxr-xr-x. 1 root root 901 May 5 00:36 libevent.la
-rw-r--r--. 1 root root 131924 May 5 00:36 libevent_openssl.a
-rwxr-xr-x. 1 root root 940 May 5 00:36 libevent_openssl.la
-rw-r--r--. 1 root root 18702 May 5 00:36 libevent_pthreads.a
-rwxr-xr-x. 1 root root 928 May 5 00:36 libevent_pthreads.la
drwxr-xr-x. 2 root root 4096 May 5 00:36 pkgconfig