libevent 是如何支持跨平台的
libevent 是如何支持跨平台的
它的跨平台表现在:
- 支持Windows、Linux、*BSD和Mac Os;
- 支持多种I/O多路复用技术, epoll、poll、dev/poll、select和kqueue等;
在include/event2/util.h下面定义了一堆条件包含,它会根据特定和宏包含特定的文件。从宏的名字来看,其指明了是否有这个头文件。有时还会指明是否有某个函数。这样做的原因很简单,因为libevent是跨平台的,必须得考虑到某些系统可能没有一些头文件或者函数。
#include <event2/event-config.h>
#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef EVENT__HAVE_STDINT_H
#include <stdint.h>
#elif defined(EVENT__HAVE_INTTYPES_H)
#include <inttypes.h>
#endif
#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef EVENT__HAVE_STDDEF_H
#include <stddef.h>
#endif
#ifdef _MSC_VER
#include <BaseTsd.h>
#endif
#include <stdarg.h>
#ifdef EVENT__HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#ifdef EVENT__HAVE_GETADDRINFO
/* for EAI_* definitions. */
#include <ws2tcpip.h>
#endif
#else
#ifdef EVENT__HAVE_ERRNO_H
#include <errno.h>
#endif
#include <sys/socket.h>
#endif
而这些宏则是在build/include/event2/event-config.h 中定义的,
/* event-config.h
*
* This file was generated by cmake when the makefiles were generated.
*
* DO NOT EDIT THIS FILE.
*
* Do not rely on macros in this file existing in later versions.
*/
#ifndef EVENT2_EVENT_CONFIG_H_INCLUDED_
#define EVENT2_EVENT_CONFIG_H_INCLUDED_
/* Numeric representation of the version */
#define EVENT__NUMERIC_VERSION 0x02010c00
#define EVENT__PACKAGE_VERSION "2.1.12"
#define EVENT__VERSION_MAJOR 2
#define EVENT__VERSION_MINOR 1
#define EVENT__VERSION_PATCH 12
/* Version number of package */
#define EVENT__VERSION "2.1.12-stable"
/* Name of package */
#define EVENT__PACKAGE "libevent"
...
#define EVENT__HAVE_SYS_TIMERFD_H 1
/* Define to 1 if you have the <sys/time.h> header file. */
#define EVENT__HAVE_SYS_TIME_H 1
这个文件是在libevent配置的时候生成的,即在编译libevent之前就应该要生成该文件了(准确来说是在cmake时),这个文件定义的宏指明了所在的系统有哪些可用的头文件、函数和一些配置。

浙公网安备 33010602011771号