linux下libevent安装配置与简介 以及 linux库文件搜索路径的配置

来源:互联网 发布:实用的防身武术 知乎 编辑:程序博客网 时间:2024/05/18 00:03

libevent简介

libevent是基于Reactor模式的I/O框架库,它具有良好的跨平台性和线程安全,它实现了统一事件源(即对I/O事件、信号和定时事件提供统一的处理)。高性能分布式内存对象缓存软件memcached是使用libevent的著名案例。

libevent下载

最新的稳定版本的下载链接点击打开链接

libevent安装

tar xzvf libevent-2.0.21-stable.tar.gz 

./configure -prefix=/usr/libevent  //安装路径

make

make install

libevent例子

编译命令

g++ test.cpp -I /usr/libevent/include/ -L/usr/libevent/lib -levent

//test.cpp

#include <event.h>#include <sys/signal.h>void signal_cb( int fd, short event, void* argc ){    struct event_base* base = ( event_base* )argc;    struct timeval delay = { 2, 0 };    printf( "Caught an interrupt signal; exiting cleanly in two seconds...\n" );    event_base_loopexit( base, &delay );}void timeout_cb( int fd, short event, void* argc ){    printf( "timeout\n" );}int main(){    struct event_base* base = event_init();    struct event* signal_event = evsignal_new( base, SIGINT, signal_cb, base );    event_add( signal_event, NULL );    timeval tv = { 1, 0 };    struct event* timeout_event = evtimer_new( base, timeout_cb, NULL );    event_add( timeout_event, &tv );    event_base_dispatch( base );    event_free( timeout_event );    event_free( signal_event );    event_base_free( base );}

注意 库文件的搜索路径 LD_LIBRARY_PATH

一般Linux系统把/lib和/usr/lib两个目录(/usr/local/lib不是)作为默认的动态库搜索路径(编译时如果不使用-static指明使用静态库的话,就会使用动态库),所以使用这两个目录中的库时不需要进行设置搜索路径即可直接使用。对于处于默认库搜索路径之外的库,需要将库的位置添加到库的搜索路径之中,本例中libevent库没在默认搜索路径中,所以需要设置库文件的搜索路径。设置linux库文件的搜索路径有下列两种方式:

1、在环境变量LD_LIBRARY_PATH中指明库的搜索路径。本例中就是 LD_LIBRARY_PATH=/usr/libevent/lib:$LD_LIBRARY_PATH

2、在/etc/ld.so.conf 文件中添加库的搜索路径。本例中就是在这个文件中添加一行 /usr/libevent/lib

   对于链接时 库文件的定位旨在ld.so.conf中添加路径就可以了,但是运行时库文件的定位还需要运行ldconfig(需要root权限)来将ld.so.conf中的路径更新到/etc/ld.so.cache中,ld.so.cache可以加快程序执行时对共享库的定位速度。

参考:http://www.360doc.com/content/12/0313/10/8093902_193931244.shtml


0 0
原创粉丝点击