1. 程式人生 > >linux下libevent安裝配置與簡介 以及 linux庫檔案搜尋路徑的配置

linux下libevent安裝配置與簡介 以及 linux庫檔案搜尋路徑的配置

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