libevent是一个c语言编写的事件框架,支持异步IO、定时器、信号事件。它支持跨平台,大部分都是在linux下安装并使用,今天介绍在unix系统minix3上的安装。
minix3上编译环境是clang,clang++,并不是我们常见的gcc,g++,所以在进行源码编译的时候,需要指定系统编译器。再一个就是minix3上没有/usr/local这个目录,如果直接configure安装不指定libevent安装前缀prefix,那么它就会安装到/usr/local目录下,这个目录并不是系统设定的,所以它的包含文件,库目录文件不会被系统搜索到,最后在使用libevent库的时候,需要我们手动添加include dir,lib dir,为了减少不必要的麻烦,我们指定configure时的前缀prefix为/usr,这样,libevent的头文件会安装到/usr/include下,而依赖库文件会安装到/usr/lib目录下。
编译安装过程:
1、下载libevent,这里官网是https://libevent.org/,下载地址是:https://github.com/libevent/libevent/releases/download/release-2.1.10-stable/libevent-2.1.10-stable.tar.gz。
2、解压
3、configure、make 、 make install安装三步曲。
这里进行configure的时候,需要注意,因为minix3.3.0版本没有gcc,g++,但是有clang,我们需要指定clang为c compiler,所以在configure的时候需要指定CC=/usr/pkg/bin/clang,我的这个机器本来已经安装了openssl,但是却找不到openssl.pc文件,所以提示没有找到openssl依赖库,只好禁用openssl,使用命令--disable-openssl
./configure --prefix=/usr CC=/usr/pkg/bin/clang --disable-openssl
这一步没有报错,就可以进行下面的编译和安装了
#make
#make install
下面的打印信息是我 make install的打印信息:
# make install
make install-am
./install-sh -c -d '/usr/bin'
/usr/bin/install -c event_rpcgen.py '/usr/bin'
./install-sh -c -d '/usr/lib'
/bin/sh ./libtool --mode=install /usr/bin/install -c libevent.la libevent_core.la libevent_extra.la libevent_pthreads.la '/usr/lib'
libtool: install: /usr/bin/install -c .libs/libevent.lai /usr/lib/libevent.la
libtool: install: /usr/bin/install -c .libs/libevent_core.lai /usr/lib/libevent_core.la
libtool: install: /usr/bin/install -c .libs/libevent_extra.lai /usr/lib/libevent_extra.la
libtool: install: /usr/bin/install -c .libs/libevent_pthreads.lai /usr/lib/libevent_pthreads.la
libtool: install: /usr/bin/install -c .libs/libevent.a /usr/lib/libevent.a
libtool: install: chmod 644 /usr/lib/libevent.a
libtool: install: ranlib /usr/lib/libevent.a
libtool: install: /usr/bin/install -c .libs/libevent_core.a /usr/lib/libevent_core.a
libtool: install: chmod 644 /usr/lib/libevent_core.a
libtool: install: ranlib /usr/lib/libevent_core.a
libtool: install: /usr/bin/install -c .libs/libevent_extra.a /usr/lib/libevent_extra.a
libtool: install: chmod 644 /usr/lib/libevent_extra.a
libtool: install: ranlib /usr/lib/libevent_extra.a
libtool: install: /usr/bin/install -c .libs/libevent_pthreads.a /usr/lib/libevent_pthreads.a
libtool: install: chmod 644 /usr/lib/libevent_pthreads.a
libtool: install: ranlib /usr/lib/libevent_pthreads.a
./install-sh -c -d '/usr/include'
/usr/bin/install -c -m 644 include/evdns.h include/event.h include/evhttp.h
include/evrpc.h include/evutil.h '/usr/include'
./install-sh -c -d '/usr/include/event2'
/usr/bin/install -c -m 644 include/event2/buffer.h include/event2/buffer_compat.h
include/event2/bufferevent.h include/event2/bufferevent_compat.h
include/event2/bufferevent_ssl.h include/event2/bufferevent_struct.h include/event2/dns.h
include/event2/dns_compat.h include/event2/dns_struct.h include/event2/event.h
include/event2/event_compat.h include/event2/event_struct.h include/event2/http.h
include/event2/http_compat.h include/event2/http_struct.h include/event2/keyvalq_struct.h
include/event2/listener.h include/event2/rpc.h include/event2/rpc_compat.h
include/event2/rpc_struct.h include/event2/tag.h include/event2/tag_compat.h
include/event2/thread.h include/event2/util.h include/event2/visibility.h
'/usr/include/event2'
./install-sh -c -d '/usr/include/event2'
/usr/bin/install -c -m 644 include/event2/event-config.h '/usr/include/event2'
./install-sh -c -d '/usr/lib/pkgconfig'
/usr/bin/install -c -m 644 libevent.pc libevent_core.pc libevent_extra.pc libevent_pthreads.pc '/usr/lib/pkgconfig'
从中可以看出头文件全部加入了/usr/include目录,库文件加入了/usr/lib目录,这样系统就能找到他们的位置了。
可以编写一个定时器来测试一下:
hello.c
#include <stdio.h>
#include <event.h>
#include <time.h>
struct event ev;
struct timeval tv;
void timer_cb(int fd, short event, void *arg)
{
printf("timer_cb\n");
event_add(&ev, &tv);
}
int main()
{
struct event_base *base = event_init();
tv.tv_sec = 1;
tv.tv_usec = 0;
event_set(&ev, -1, 0, timer_cb, NULL);
event_base_set(base, &ev);
event_add(&ev, &tv);
event_base_dispatch(base);
return 0;
}
编译:这里就需要手动设定依赖库
#clang -o hello hello.c -levent
编译运行打印信息:
# clang -o hello hello.c
/var/tmp/hello-2fbefe.o: In function `timer_cb':
hello.c:(.text+0x43): undefined reference to `event_add'
/var/tmp/hello-2fbefe.o: In function `main':
hello.c:(.text+0x81): undefined reference to `event_init'
hello.c:(.text+0xcc): undefined reference to `event_set'
hello.c:(.text+0xe1): undefined reference to `event_base_set'
hello.c:(.text+0xfc): undefined reference to `event_add'
hello.c:(.text+0x10a): undefined reference to `event_base_dispatch'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
# clang -o hello hello.c -levent
# ./hello
timer_cb
timer_cb
timer_cb
timer_cb
^C
#
不设定依赖库,编译报错;若编译通过直接运行,控制台会间隔1秒打印timer_cb。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)