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

目录
相关文章
|
2月前
|
Linux 数据安全/隐私保护 Windows
Linux共享Windows目录
Linux共享Windows目录
|
2月前
|
Linux Windows
双系统安装:一键解锁电脑新境界,Windows与Linux并肩作战!
告别单一选择,拥抱无限可能!厌倦了单一操作系统的束缚吗?现在就打破常规,同时享受Windows的便捷与Linux的强大吧!🚀想象早晨用Windows高效办公,夜晚切换至Linux探索开源世界,是不是心动了?💖双系统安装就像赋予电脑双重人格,让一台机器拥有两种独立环境,自由切换如同拥有两台电脑!💰只需三步:规划分区、准备安装介质、安装Linux,即可轻松完成设置,立即体验双重身份带来的便利吧!💾💻
36 0
|
2月前
|
缓存 NoSQL Linux
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
|
2月前
|
Unix Linux 开发工具
linux笔记 diff及patch的制作与使用
这篇文章是关于Linux系统中使用`diff`命令生成补丁文件以及使用`patch`命令应用这些补丁的详细教程和实战案例。
31 2
linux笔记 diff及patch的制作与使用
|
1月前
|
Linux
用clang编译Linux内核
用clang编译Linux内核
|
2月前
|
Unix Linux Ruby
在windows和linux上高效快捷地发布Dash应用
在windows和linux上高效快捷地发布Dash应用
|
2月前
|
Ubuntu Linux 虚拟化
安装Windows Linux 子系统的方法:适用于windows 11 版本
本文提供了在Windows 11系统上安装Linux子系统(WSL)的详细步骤,包括启用子系统和虚拟化功能、从Microsoft Store安装Linux发行版、设置WSL默认版本、安装WSL2补丁,以及完成Ubuntu的首次安装设置。
122 2
|
2月前
|
Linux C语言
深度探索Linux操作系统 —— 编译过程分析
深度探索Linux操作系统 —— 编译过程分析
20 2
|
2月前
|
Linux Windows
【Linux】grub命令行引导进入windows系统
【8月更文挑战第20天】在Linux中通过GRUB命令行引导Windows的方法包括:1) 进入GRUB命令行模式,启动时按`c`键;2) 使用`ls`查找含Windows引导文件的分区,如`bootmgr`或`ntldr`;3) 设置根设备`root=(hd0,msdos3)`与链加载器`chainloader +1`;4) 输入`boot`命令启动Windows。请注意实际步骤可能因系统配置而异。
|
2月前
|
数据采集 Linux
Linux源码阅读笔记20-PCI设备驱动详解
Linux源码阅读笔记20-PCI设备驱动详解
下一篇
无影云桌面