1 编写编译脚本
vim build.sh
#!/bin/bash
# 检查是否退出
set -e
# 下载编译代码
libevent_version=libevent-2.1.12-stable
wget -O ${libevent_version}.tar.gz https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
tar zxvf ${libevent_version}.tar.gz
# 新建编译目录
build_dir=${libevent_version}/build
if [ ! -d "$build_dir" ]; then
echo "Create ${build_dir}..."
mkdir -p ${build_dir}
fi
cd ${build_dir}
# 编译算法库
echo "Building Event ..."
cmake -D CMAKE_INSTALL_PREFIX=./install ..
make -j$(nproc)
# 安装算法库
make install
2 编写测试程序
2.1 移动编译库到指定路径
mv libevent-2.1.12-stable/build/install/lib ../../lib/libevent/
mv libevent-2.1.12-stable/build/install/include ../../lib/libevent/
cd ../../
2.2 修改Cmake的配置文件
vim lib/libevent/lib/cmake/libevent/LibeventTargets-shared.cmake
# The installation prefix configured by this project.
# set(_IMPORT_PREFIX "path/to/libevent-2.1.12-stable/build/install")
# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
vim lib/libevent/lib/cmake/libevent/LibeventTargets-static.cmake
# The installation prefix configured by this project.
# set(_IMPORT_PREFIX "path/to/libevent-2.1.12-stable/build/install")
# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
2.3 编写Cmake的编译文件
vim CMakeLists.txt
# 设置编译最小版本
cmake_minimum_required(VERSION 3.6.0)
# 设置目标工程名称
project(test)
# 设置编译工具参数
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Release)
# 设置输出程序路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/output)
# 添加第三方链接库
set(Libevent_DIR ${PROJECT_SOURCE_DIR}/lib/libevent/lib/cmake/libevent)
find_package(Libevent REQUIRED)
include_directories(${LIBEVENT_INCLUDE_DIRS})
message(STATUS "Libevent library status:")
message(STATUS " version: ${LIBEVENT_VERSION}")
message(STATUS " libraries: ${LIBEVENT_LIBRARIES}")
message(STATUS " include path: ${LIBEVENT_INCLUDE_DIRS}")
# 生成可执行的程序
add_executable(${CMAKE_PROJECT_NAME} main.cpp)
# 程序相关的链接库
target_link_libraries(${CMAKE_PROJECT_NAME} ${LIBEVENT_LIBRARIES})
2.4 编写main.cpp测试程序
vim main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <event2/event.h>
// 输入回调函数
void stdin_read_cb(evutil_socket_t fd, short events, void *arg)
{
char buf[256];
int len = read(fd, buf, sizeof(buf) - 1);
if (len == -1)
{
perror("read error");
return;
}
else if (len == 0)
{
printf("EOF\n");
struct event_base *base = (struct event_base *)arg;
event_base_loopbreak(base);
return;
}
buf[len] = '\0';
printf("Read from stdin: %s", buf);
}
// 定时回调函数
void timeout_cb(evutil_socket_t fd, short events, void *arg)
{
printf("Timeout!\n");
}
int main(int argc, char **argv)
{
// 创建事件对象
struct event_base *base = event_base_new();
if (!base)
{
fprintf(stderr, "Could not initialize libevent!\n");
return 1;
}
// 创建监听事件
struct event *stdin_event = event_new(base, fileno(stdin), EV_READ | EV_PERSIST, stdin_read_cb, (void *)base);
if (!stdin_event)
{
fprintf(stderr, "Could not create stdin event!\n");
event_base_free(base);
return 1;
}
event_add(stdin_event, NULL);
// 创建定时事件
struct event *timeout_event = evtimer_new(base, timeout_cb, NULL);
if (!timeout_event)
{
fprintf(stderr, "Could not create timeout event!\n");
event_free(stdin_event);
event_base_free(base);
return 1;
}
// 设置五秒超时
struct timeval timeout = {5, 0};
evtimer_add(timeout_event, &timeout);
// 进入事件循环
event_base_dispatch(base);
// 清理事件对象
event_free(stdin_event);
event_free(timeout_event);
event_base_free(base);
return 0;
}
2.5 编译运行测试程序
# 编译测试程序
rm -rf build && mkdir build && cd build && cmake .. && make
# 运行测试程序
./output/test
参考网址
- libevent
- Building and installing Libevent