如何在Ubuntu中安装libevent库
1.下载
下载地址:http://libevent.org/
2.解压
tar -zxvf libevent-2.1.11-stable.tar.gz
3.配置安装路径
cd libevent-2.1.11-stable
./configure --prefix=/usr
如果不设置prefix,则默认为**/usr/local/lib**,这里建议如上设置。
4.编译安装
如果提示错误:
configure: error: openssl is a must but can not be found. You should add the directory containing `openssl.pc' to the `PKG_CONFIG_PATH' environment variable, or set `CFLAGS' and `LDFLAGS' directly for openssl, or use `--disable-openssl' to disable support for openssl encryption
一般为openssl库的缘故,需要安装openssl,一般ubuntu自带openssl,这里需要openssl-devel,要注意的是在ubuntu中需要使用
sudo apt-get install libssl-dev来安装而不是sudo apt-get install openssl-devel。
5.测试是否安装成功
ls -al /usr/local/lib | grep libevent
6.安装后,使用库编译时包含event.h时,提示未定义的引用
需要在编译命令后加上 -levent。具体原因
7.编译为可执行文件后,执行文件提示找不到共享库,原因可能如下:
7.1 操作系统中没有正确安装相应共享库,这种情况就说明前几步没有安装成功。
7.2 已经安装了该共享库, 但 执行需要调用该共享库的程序的时候, 程序按照默认共享库路径找不到该共享库文件.
这种情况的解决方法:
1)如果共享库文件安装到了/lib或/usr/lib目录下, 那么需执行一下 ldconfig 命令
ldconfig命令的用途, 主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下, 搜索出可共享的动态链接库(格式如lib*.so*), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为/etc/ld.so.cache, 此文件保存已排好序的动态链接库名字列表.
2)如果共享库文件安装到了/usr/local/lib(很多开源的共享库都会安装到该目录下)或其它"非/lib或/usr/lib"目录下, 那么在执行ldconfig命令前, 还要把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 如下:
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig