版本简述
4.29之后增加了io_uring支持.
4.26-4.28期间增加与完善Linux AIO支持.
4.25之后增加了Android的兼容性支持.
4.00之后Linux支持signalfd、eventfd、timerfd
命名规则
我们一般根据版本号来确定当前使用的libev版本, 打包后的文件命名规则是以libev-${version}.tar.gz形式出现.
注意: 不建议大家激进的跟进新版本, 最新的版本也不一定就是最好的.
在线阅读 可以从译者在这里提供的fork源在线阅读.
下载源码 推荐大家从Libev作者本人提供的服务器下载源码包, 这样下载的安全性也是有保障的.
注意: 鉴于国际网络互通的一些问题导致有些同学可能无法访问. 所以本站已缓存2015年以来的稳定版打包文件, 如有需要可以直接点击链接进行下载:
libev-4.33.tar.gz
libev-4.27.tar.gz
libev-4.25.tar.gz
libev-4.24.tar.gz
libev-4.23.tar.gz
libev-4.22.tar.gz
libev-4.20.tar.gz
libev-4.19.tar.gz
libev-4.18.tar.gz
编译安装1. 下载源码包 为了保证下载速度, 我们选择上述链接中的4.33版本下载到本地:
12345678910111213root@iZbp18k2vy63cz9njzffe8Z:~/build# wget https://libev.cn/downloads/libev-4.33.tar.gz--2022-03-14 21:49:43-- https://libev.cn/downloads/libev-4.33.tar.gzResolving libev.cn (libev.cn)... 185.199.108.153, 185.199.110.153, 185.199.109.153, ...Connecting to libev.cn (libev.cn)|185.199.108.153|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 569527 (556K) [application/gzip]Saving to: ‘libev-4.33.tar.gz’libev-4.33.tar.gz 100%[======================>] 556.18K 36.8KB/s in 18s2022-03-14 21:50:03 (31.1 KB/s) - ‘libev-4.33.tar.gz’ saved [569527/569527]root@iZbp18k2vy63cz9njzffe8Z:~/build#
2. 安装依赖 为了运行configure, 需要安装以下工具:
autoconf
automake
libtool
make
gcc/clang
3. 开始编译
运行sh autogen.sh
12345678910root@iZbp18k2vy63cz9njzffe8Z:~/build/libev-4.33# sh autogen.shlibtoolize: putting auxiliary files in '.'.libtoolize: linking file './ltmain.sh'libtoolize: Consider adding 'AC_CONFIG_MACRO_DIRS([m4])' to configure.ac,libtoolize: and rerunning libtoolize and aclocal.libtoolize: Consider adding '-I m4' to ACLOCAL_AMFLAGS in Makefile.am.configure.ac:12: installing './compile'configure.ac:7: installing './missing'Makefile.am: installing './depcomp'root@iZbp18k2vy63cz9njzffe8Z:~/build/libev-4.33#
运行./configure
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130root@iZbp18k2vy63cz9njzffe8Z:~/build/libev-4.33# ./configurechecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /usr/bin/mkdir -pchecking for gawk... nochecking for mawk... mawkchecking whether make sets $(MAKE)... yeschecking whether make supports nested variables... yeschecking whether to enable maintainer-specific portions of Makefiles... nochecking for gcc... gccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables...checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking whether gcc understands -c and -o together... yeschecking whether make supports the include directive... yes (GNU style)checking dependency style of gcc... gcc3checking build system type... x86_64-pc-linux-gnuchecking host system type... x86_64-pc-linux-gnuchecking how to print strings... printfchecking for a sed that does not truncate output... /usr/bin/sedchecking for grep that handles long lines and -e... /usr/bin/grepchecking for egrep... /usr/bin/grep -Echecking for fgrep... /usr/bin/grep -Fchecking for ld used by gcc... /usr/bin/ldchecking if the linker (/usr/bin/ld) is GNU ld... yeschecking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -Bchecking the name lister (/usr/bin/nm -B) interface... BSD nmchecking whether ln -s works... yeschecking the maximum length of command line arguments... 1572864checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noopchecking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noopchecking for /usr/bin/ld option to reload object files... -rchecking for objdump... objdumpchecking how to recognize dependent libraries... pass_allchecking for dlltool... nochecking how to associate runtime and link libraries... printf %s\nchecking for ar... archecking for archiver @FILE support... @checking for strip... stripchecking for ranlib... ranlibchecking command to parse /usr/bin/nm -B output from gcc object... okchecking for sysroot... nochecking for a working dd... /usr/bin/ddchecking how to truncate binary pipes... /usr/bin/dd bs=4096 count=1checking for mt... mtchecking if mt is a manifest tool... nochecking how to run the C preprocessor... gcc -Echecking for ANSI C header files... yeschecking for sys/types.h... yeschecking for sys/stat.h... yeschecking for stdlib.h... yeschecking for string.h... yeschecking for memory.h... yeschecking for strings.h... yeschecking for inttypes.h... yeschecking for stdint.h... yeschecking for unistd.h... yeschecking for dlfcn.h... yeschecking for objdir... .libschecking if gcc supports -fno-rtti -fno-exceptions... nochecking for gcc option to produce PIC... -fPIC -DPICchecking if gcc PIC flag -fPIC -DPIC works... yeschecking if gcc static flag -static works... yeschecking if gcc supports -c -o file.o... yeschecking if gcc supports -c -o file.o... (cached) yeschecking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yeschecking whether -lc should be explicitly linked in... nochecking dynamic linker characteristics... GNU/Linux ld.sochecking how to hardcode library paths into programs... immediatechecking whether stripping libraries is possible... yeschecking if libtool supports shared libraries... yeschecking whether to build shared libraries... yeschecking whether to build static libraries... yeschecking sys/inotify.h usability... yeschecking sys/inotify.h presence... yeschecking for sys/inotify.h... yeschecking sys/epoll.h usability... yeschecking sys/epoll.h presence... yeschecking for sys/epoll.h... yeschecking sys/event.h usability... nochecking sys/event.h presence... nochecking for sys/event.h... nochecking port.h usability... nochecking port.h presence... nochecking for port.h... nochecking poll.h usability... yeschecking poll.h presence... yeschecking for poll.h... yeschecking sys/timerfd.h usability... yeschecking sys/timerfd.h presence... yeschecking for sys/timerfd.h... yeschecking sys/select.h usability... yeschecking sys/select.h presence... yeschecking for sys/select.h... yeschecking sys/eventfd.h usability... yeschecking sys/eventfd.h presence... yeschecking for sys/eventfd.h... yeschecking sys/signalfd.h usability... yeschecking sys/signalfd.h presence... yeschecking for sys/signalfd.h... yeschecking linux/aio_abi.h usability... yeschecking linux/aio_abi.h presence... yeschecking for linux/aio_abi.h... yeschecking linux/fs.h usability... yeschecking linux/fs.h presence... yeschecking for linux/fs.h... yeschecking for inotify_init... yeschecking for epoll_ctl... yeschecking for kqueue... nochecking for port_create... nochecking for poll... yeschecking for select... yeschecking for eventfd... yeschecking for signalfd... yeschecking for clock_gettime... yeschecking for nanosleep... yeschecking for __kernel_rwf_t... yeschecking for library containing floor... -lmchecking that generated files are newer than configure... doneconfigure: creating ./config.statusconfig.status: creating Makefileconfig.status: creating config.hconfig.status: executing depfiles commandsconfig.status: executing libtool commandsroot@iZbp18k2vy63cz9njzffe8Z:~/build/libev-4.33#
运行make && make install
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758root@iZbp18k2vy63cz9njzffe8Z:~/build/libev-4.33# make && make installmake all-ammake[1]: Entering directory '/root/build/libev-4.33'/bin/bash ./libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -g -O3 -MT ev.lo -MD -MP -MF .deps/ev.Tpo -c -o ev.lo ev.clibtool: compile: gcc -DHAVE_CONFIG_H -I. -g -O3 -MT ev.lo -MD -MP -MF .deps/ev.Tpo -c ev.c -fPIC -DPIC -o .libs/ev.oev.c:2143:31: warning: ‘ev_default_loop_ptr’ initialized and declared ‘extern’ EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ ^~~~~~~~~~~~~~~~~~~libtool: compile: gcc -DHAVE_CONFIG_H -I. -g -O3 -MT ev.lo -MD -MP -MF .deps/ev.Tpo -c ev.c -o ev.o >/dev/null 2>&1mv -f .deps/ev.Tpo .deps/ev.Plo/bin/bash ./libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -g -O3 -MT event.lo -MD -MP -MF .deps/event.Tpo -c -o event.lo event.clibtool: compile: gcc -DHAVE_CONFIG_H -I. -g -O3 -MT event.lo -MD -MP -MF .deps/event.Tpo -c event.c -fPIC -DPIC -o .libs/event.olibtool: compile: gcc -DHAVE_CONFIG_H -I. -g -O3 -MT event.lo -MD -MP -MF .deps/event.Tpo -c event.c -o event.o >/dev/null 2>&1mv -f .deps/event.Tpo .deps/event.Plo/bin/bash ./libtool --tag=CC --mode=link gcc -g -O3 -version-info 4:0:0 -o libev.la -rpath /usr/local/lib ev.lo event.lo -lmlibtool: link: gcc -shared -fPIC -DPIC .libs/ev.o .libs/event.o -lm -g -O3 -Wl,-soname -Wl,libev.so.4 -o .libs/libev.so.4.0.0libtool: link: (cd ".libs" && rm -f "libev.so.4" && ln -s "libev.so.4.0.0" "libev.so.4")libtool: link: (cd ".libs" && rm -f "libev.so" && ln -s "libev.so.4.0.0" "libev.so")libtool: link: ar cru .libs/libev.a ev.o event.oar: 'u' modifier ignored since 'D' is the default (see 'U')libtool: link: ranlib .libs/libev.alibtool: link: ( cd ".libs" && rm -f "libev.la" && ln -s "../libev.la" "libev.la" )make[1]: Leaving directory '/root/build/libev-4.33'make[1]: Entering directory '/root/build/libev-4.33' /usr/bin/mkdir -p '/usr/local/lib' /bin/bash ./libtool --mode=install /usr/bin/install -c libev.la '/usr/local/lib'libtool: install: /usr/bin/install -c .libs/libev.so.4.0.0 /usr/local/lib/libev.so.4.0.0libtool: install: (cd /usr/local/lib && { ln -s -f libev.so.4.0.0 libev.so.4 || { rm -f libev.so.4 && ln -s libev.so.4.0.0 libev.so.4; }; })libtool: install: (cd /usr/local/lib && { ln -s -f libev.so.4.0.0 libev.so || { rm -f libev.so && ln -s libev.so.4.0.0 libev.so; }; })libtool: install: /usr/bin/install -c .libs/libev.lai /usr/local/lib/libev.lalibtool: install: /usr/bin/install -c .libs/libev.a /usr/local/lib/libev.alibtool: install: chmod 644 /usr/local/lib/libev.alibtool: install: ranlib /usr/local/lib/libev.alibtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin" ldconfig -n /usr/local/lib----------------------------------------------------------------------Libraries have been installed in: /usr/local/libIf you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libtool, andspecify the full pathname of the library, or use the '-LLIBDIR'flag during linking and do at least one of the following: - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the 'LD_RUN_PATH' environment variable during linking - use the '-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to '/etc/ld.so.conf'See any operating system documentation about shared libraries formore information, such as the ld(1) and ld.so(8) manual pages.---------------------------------------------------------------------- /usr/bin/mkdir -p '/usr/local/include' /usr/bin/install -c -m 644 ev.h ev++.h event.h '/usr/local/include' /usr/bin/mkdir -p '/usr/local/share/man/man3' /usr/bin/install -c -m 644 ev.3 '/usr/local/share/man/man3'make[1]: Leaving directory '/root/build/libev-4.33'root@iZbp18k2vy63cz9njzffe8Z:~/build/libev-4.33#
注意: 上述编译期间如无报错, 那么最终头文件与库文件都将会被安装的/usr/local的相关目录下.
测试运行 为了检查我们是否正常安装好, 可以就地编写一个简单的demo来进行测试:
123456789101112131415161718192021#include
上述示例注册空闲(idle)事件后进入到事件循环内部, 空闲(idle)事件回调打印输出后立刻调用方法停止.
由于loop内部再未注册任何其它事件, 所以ev_run将在停止后返回并且整个进程正常退出.
代码完成之后我们手动指定头文件与库文件的路径然后编译运行:
1234root@iZbp18k2vy63cz9njzffe8Z:~/build# cc -o main main.c -L/usr/local/lib -I/usr/local/include/ -lev -Wl,-rpath,/usr/local/libroot@iZbp18k2vy63cz9njzffe8Z:~/build# ./mainidle start.root@iZbp18k2vy63cz9njzffe8Z:~/build#
可以看到程序执行成功, Libev已经本成功安装好了.