前言:
producer使用libevent接收http post请求
libevent下载:
官网:
http://libevent.org/
版本: libevent-2.0.22-stable.tar.gz
有时候从该网站无法进行下载,因此可以通过如下链接下载:
https://sourceforge.net/projects/levent/?source=typ_redirect
解压:
tar -xvf libevent-2.0.22-stable.tar.gz
配置:
解压后进入目录,进行配置,把库安装到/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