Quick start | Libevent

1.下载Libevent源码

$ git clone https://github.com/libevent/libevent.git

 

2. 创建Libevent本地安装目录 并 添加本地bin目录到环境变量PATH

$ export MY_INSTALL_DIR=$HOME/.libevent
$ mkdir -p $MY_INSTALL_DIR
$ export PATH="$MY_INSTALL_DIR/bin:$PATH

 

3. 构建并安装Libenvet

$ cd libevent
$ mkdir -p cmake/build
$ cd cmake/build
$ cmake -DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR ../..
$ make
$ make install

 

4. 编译Hello world示例程序

设置pkg config查找路径:

$ export PKG_CONFIG_PATH=/home/suph/.libevent/lib/pkgconfig/

创建Makefile文件:

#Makefile
CXX = gcc CPPFLAGS += `pkg-config --cflags libevent_core libevent_extra` LDFLAGS += -L/usr/local/lib `pkg-config --libs libevent_core libevent_extra`\ -pthread all: hello-world hello-world: hello-world.o $(CXX) $^ $(LDFLAGS) -o $@ clean: rm -f *.o hello-world

 

5. 运行示例程序

直接运行hello-word例程,报如下错误:

$ ./hello-world 
./hello-world: error while loading shared libraries: libevent_core-2.1.so.7: cannot open shared object file: No such file or directory

产生原因:

    在运行时,程序无法找到libevent_core-2.1.so.7这个动态库。因为该动态库在安装时,存放的路径在/home/suph/.libevent/lib下,不在系统的默认查找路径内。

解决方法:

    将Libevent安装路径 加入到 系统查找路径内

    在/etc/ld.so.conf.d/目录下,新增mylib.conf文件,内容如下

$ cat /etc/ld.so.conf.d/mylib.conf 
# library path configuration for dynamic library
/home/suph/.libevent/lib

 

posted @ 2023-03-18 21:55  suphgcm  阅读(28)  评论(0编辑  收藏  举报