libevent2笔记(linux、windows、android的编译)

简介: 0. 前言我使用的版本是libevent-2.0.21-stable。高级的应用还是得看官网文档http://www.wangafu.net/~nickm/libevent-2.0/doxygen/html/1. 功能总结libevent的核心作用是实现消息循环、消息队列管理与回调,可用来监听文件(socket也算文件)属性变化、超时、锁状态变化,其中超时可以用作Timer。

0. 前言

我使用的版本是libevent-2.0.21-stable。高级的应用还是得看官网文档http://www.wangafu.net/~nickm/libevent-2.0/doxygen/html/

1. 功能总结

libevent的核心作用是实现消息循环、消息队列管理与回调,可用来监听文件(socket也算文件)属性变化、超时、锁状态变化,其中超时可以用作Timer。
额外的功能是作为HTTP的server或client。

2. 编译

2.1 Linux版编译

在目录下
./configure && make
即可在./.lib/下得到5个.a静态库。 不确定是否在此之前我已安装好各种依赖库所以没遇到任何障碍

liuhx@uc ~/Downloads/libevent-2.0.21-stable/.libs $ ll *.a
-rw-r--r-- 1 liuhx liuhx 2309114 Feb 17 13:38 libevent.a
-rw-r--r-- 1 liuhx liuhx 1431730 Feb 17 13:38 libevent_core.a
-rw-r--r-- 1 liuhx liuhx  877456 Feb 17 13:38 libevent_extra.a
-rw-r--r-- 1 liuhx liuhx  195868 Feb 17 13:38 libevent_openssl.a
-rw-r--r-- 1 liuhx liuhx   21998 Feb 17 13:38 libevent_pthreads.a
查看Makefile文件的内容,可得知4个静态库对应的源文件:

CORE_SRC = event.c evthread.c buffer.c \
	bufferevent.c bufferevent_sock.c bufferevent_filter.c \
	bufferevent_pair.c listener.c bufferevent_ratelim.c \
	evmap.c	log.c evutil.c evutil_rand.c strlcpy.c $(SYS_SRC)
EXTRA_SRC = event_tagging.c http.c evdns.c evrpc.c
libevent_pthreads_la_SOURCES = evthread_pthread.c
libevent_openssl_la_SOURCES = bufferevent_openssl.c
即libevent_core.a里是核心功能,其中$(SYS_SRC)在各个平台会不同,linux下是select.c、poll.c、epoll.c、signal.c等,windows下是win32select.c evthread_win32.c等,不一一列举了。libevent_extra.a里包含http、dns等功能。另外两个libevent_*.a就见名知意了。而libevent.a是libevent_core.a和libevent_extra.a的集合。

2.2 Windows版编译

可以用VS的Command Prompt(开始菜单->Visual Studio 2013->Visual Studio Tools->VS2013 x64 Native Tools Command Prompt,VS和CPU版本应对应到你所用的)在libevent目录下
nmake Makefile.nmake
就能得到

2015/02/26  10:40           336,040 libevent_extras.lib
2015/02/26  10:40           789,110 libevent.lib
2015/02/26  10:40           453,366 libevent_core.lib
3个静态库文件。

不过一般会习惯做成VS工程。所以新建一个VS静态库工程,对着Makefile.nmake的内容添加源文件、引用目录、预编译命令就行了。vcproj的部分内容如下:

<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\buffer.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\bufferevent.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\bufferevent_async.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\bufferevent_filter.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\bufferevent_pair.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\bufferevent_ratelim.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\bufferevent_sock.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\buffer_iocp.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\evdns.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\event.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\event_iocp.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\event_tagging.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\evmap.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\evrpc.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\evthread.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\evthread_win32.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\evutil.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\evutil_rand.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\http.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\listener.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\log.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\signal.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\strlcpy.c" />
<ClCompile Include="..\..\..\third_party\libevent-2.0.21-stable\win32select.c" />

windows平台在链接时需要加入ws2_32.lib和wsock32.lib

2.3 Android版编译

若是能看懂linux和windows的Makefile,那是很容易写好Android.mk的。但在此之前需要生成好android版的config.h和event-config.h。

目录下运行(ndk版本随意,请替换成对应的路径):

SYSROOT=~/Applications/android-ndk-r8e/platforms/android-8/arch-arm
./configure --host=arm-linux-androideabi CC=~/Applications/android-ndk-r8e/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc CFLAGS=--sysroot=$SYSROOT

这样就会生成好config.h了。然后再
make
过程中会调用sed修改好event-config.h。顺利的话会生成四个.a

liuhx@uc ~/Downloads/libevent-2.0.21-stable/.libs $ ll *.a
-rw-r--r-- 1 liuhx liuhx 455082 Feb 26 14:59 libevent.a
-rw-r--r-- 1 liuhx liuhx 267398 Feb 26 14:59 libevent_core.a
-rw-r--r-- 1 liuhx liuhx 187756 Feb 26 14:59 libevent_extra.a
-rw-r--r-- 1 liuhx liuhx   4014 Feb 26 14:59 libevent_pthreads.a
因为没有编译OpenSSL,所以不会有libevent_openssl.a。

前面的过程会弄好config.h和event-config.h,其中event-config.h是对应android版本的配置,是必须的,不然编不过,所以无法一上来就用Android.mk。过了make这一步才行,也就是要保留修改过的event-config.h来使用下面的Android.mk

LOCAL_PATH := $(call my-dir)/..
include $(CLEAR_VARS)
LOCAL_MODULE := libevent

LOCAL_SRC_FILES := \
    event.c \
    evutil.c \
    epoll.c \
    log.c \
    poll.c \
    select.c \
    signal.c \
    http.c \
    buffer.c \
    evthread.c \
    evmap.c \
    bufferevent.c \
    listener.c \
    evutil_rand.c \
    bufferevent_sock.c \
    bufferevent_filter.c \
    bufferevent_pair.c \
    strlcpy.c \
    event_tagging.c \
    evrpc.c \
    bufferevent_ratelim.c \
    evdns.c \
    evthread_pthread.c

LOCAL_C_INCLUDES := \
	$(LOCAL_PATH) \
    $(LOCAL_PATH)/include \
    $(LOCAL_PATH)/compat

LOCAL_CFLAGS := -DHAVE_CONFIG_H

include $(BUILD_SHARED_LIBRARY)
注意最后一行在集成到app时应该把BUILD_SHARED_LIBRARY应该替换成BUILD_STATIC_LIBRARY,这里仅为了编译。

转载请注明出处:http://blog.csdn.net/hursing

目录
相关文章
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
安全 Linux Shell
全面对比linux和windows,选择哪个系统比较好
全面对比linux和windows,选择哪个系统比较好
65 0
|
1月前
|
Unix 编译器 开发者
Qt5.14.2 轻松掌握Qt中的压缩与解压缩:QuaZIP的神秘面纱与实战演练之windows环境编译
Qt5.14.2 轻松掌握Qt中的压缩与解压缩:QuaZIP的神秘面纱与实战演练之windows环境编译
|
1月前
|
算法 Linux
【Linux笔记】压缩、解压文件的 4 种方式。tar、gzip、gunzip、zip、unzip、7z命令使用方法
【Linux笔记】压缩、解压文件的 4 种方式。tar、gzip、gunzip、zip、unzip、7z命令使用方法
|
1天前
|
Linux Shell Android开发
自动化脚本之GPIO/LED相关适用于Android/Linux
自动化脚本之GPIO/LED相关适用于Android/Linux
8 0
|
1天前
|
Linux Android开发
Linux(6)CH9434 SPI调试笔记
Linux(6)CH9434 SPI调试笔记
7 0
|
1天前
|
Linux
Linux(5)WIFI/BT调试笔记
Linux(5)WIFI/BT调试笔记
10 0
|
2天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
20 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
8天前
|
SQL 监控 安全
Linux&Windows 日志分析 陇剑杯 CTF
Linux&Windows 日志分析 陇剑杯 CTF
33 0
|
11天前
|
Linux Windows
Windows、Mac、Linux解决端口被占用的问题
Windows、Mac、Linux解决端口被占用的问题
19 1