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

时间:2020-12-15 00:16:54

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