libevent安装
- libevent : 名气最大,应用最广泛,历史悠久的跨平台事件库;
libev : 较libevent而言,设计更简练,性能更好,但对Windows支持不够好;
libuv : 开发node的过程中需要一个跨平台的事件库,他们首选了libev,但又要支持Windows,故重新封装了一套,*nix下用libev实现,Windows下用IOCP实现;
win7上安装:
Libevent官网:http://libevent.org/
windows 7下编译:
编译环境: windows 7 + VS2010
(1)解压libevent到F:\libevent\libevent-2.0.21-stable
(2)打开Microsoft visual studio 2010命令行工具
(3)修改以下三个文件,添加宏定义:
在以下3个文件开头添加“#define _WIN32_WINNT 0x0500”
libevent-2.0.21-stable\event_iocp.c
libevent-2.0.21-stable\evthread_win32.c
libevent-2.0.21-stable\listener.c
(4)使用VS命令提示工具编译:
cd/d F:\libevent\libevent-2.0.21-stable
nmake /f Makefile.nmake
生成的“libevent.lib”、“libevent_core.lib”、“libevent_extras.lib”三个文件就是我们需要的 Libevent 静态链接库。
libevent_core.lib:All core event and buffer functionality. This library contains all the event_base, evbuffer, bufferevent, and utility functions.
libevent_extras.lib:This library defines protocol-specific functionality that you may or may not want for your application, including HTTP, DNS, and RPC.
libevent.lib:This library exists for historical reasons; it contains the contents of both libevent_core and libevent_extra. You shouldn’t use it; it may go away in a future version of Libevent.
VS2010下使用lib
新建一个VC++控制台项目:
环境配置:
项目下建一个Lib目录,将上面三个lib文件copy到该目录下。
新建一个Include目录,将F:\libevent\libevent-2.0.21-stable\include下的文件和文件夹copy到该目录下,F:\libevent\libevent-2.0.21-stable\WIN32-Code下的文件copy到该目录下,2个event2目录下的文件可合并一起。
编译代码我 总是提示:
无法解析的外部符号 _event_base_new
无法解析的外部符号 _event_new
等,这是为什么,我确实已经设置lib了。为什么还是找不到?
后来发现我用的libevent是用namke vs是x64编译的,而我的项目的版本是32位的,用libevent的32库的lib就可以了。
这说明在vs中用到lib时编译的版本必须一致,否则无法识别。
main代码:
- // LibeventTest.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <string.h>
- #include <errno.h>
- #include <stdio.h>
- #include <signal.h>
- #ifndef WIN32
- #include <netinet/in.h>
- # ifdef _XOPEN_SOURCE_EXTENDED
- # include <arpa/inet.h>
- # endif
- #include <sys/socket.h>
- #endif
- #include "event2/bufferevent.h"
- #include "event2/buffer.h"
- #include "event2/listener.h"
- #include "event2/util.h"
- #include "event2/event.h"
- #include <WinSock2.h>
- static const char MESSAGE[] = "Hello, World!\n";
- static const int PORT = ;
- static void conn_writecb(struct bufferevent *bev, void *user_data)
- {
- struct evbuffer *output = bufferevent_get_output(bev);
- if (evbuffer_get_length(output) == )
- {
- printf("flushed answer\n");
- bufferevent_free(bev);
- }
- }
- static void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
- {
- if (events & BEV_EVENT_EOF)
- {
- printf("Connection closed.\n");
- }
- else if (events & BEV_EVENT_ERROR)
- {
- printf("Got an error on the connection: %s\n",
- strerror(errno));/*XXX win32*/
- }
- /* None of the other events can happen here, since we haven't enabled
- * timeouts */
- bufferevent_free(bev);
- }
- static void signal_cb(evutil_socket_t sig, short events, void *user_data)
- {
- struct event_base *base = (struct event_base *)user_data;
- struct timeval delay = { , };
- printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");
- event_base_loopexit(base, &delay);
- }
- static void listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
- struct sockaddr *sa, int socklen, void *user_data)
- {
- struct event_base *base = (struct event_base *)user_data;
- struct bufferevent *bev;
- bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
- if (!bev)
- {
- fprintf(stderr, "Error constructing bufferevent!");
- event_base_loopbreak(base);
- return;
- }
- bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL);
- bufferevent_enable(bev, EV_WRITE);
- bufferevent_disable(bev, EV_READ);
- bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
- }
- int main(int argc, char **argv)
- {
- struct event_base *base;
- struct evconnlistener *listener;
- struct event *signal_event;
- struct sockaddr_in sin;
- #ifdef WIN32
- WSADATA wsa_data;
- WSAStartup(0x0201, &wsa_data);
- #endif
- base = event_base_new();
- if (!base)
- {
- fprintf(stderr, "Could not initialize libevent!\n");
- return ;
- }
- memset(&sin, , sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_port = htons(PORT);
- listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
- LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -,
- (struct sockaddr*)&sin,
- sizeof(sin));
- if (!listener)
- {
- fprintf(stderr, "Could not create a listener!\n");
- return ;
- }
- signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
- if (!signal_event || event_add(signal_event, NULL)<)
- {
- fprintf(stderr, "Could not create/add a signal event!\n");
- return ;
- }
- event_base_dispatch(base);
- evconnlistener_free(listener);
- event_free(signal_event);
- event_base_free(base);
- printf("done\n");
- return ;
- }
项目属性设置:
VC++目录:
包含目录,添加:F:\Projects\LibeventTest\LibeventTest\Include;
库目录,添加:F:\Projects\LibeventTest\LibeventTest\Lib;
C/C++:
代码生成-->运行库:多线程调试 (/MTd)(Debug下),多线程 (/MT)(Release下)
连接器:
输入:ws2_32.lib;wsock32.lib;libevent.lib;libevent_core.lib;libevent_extras.lib;
ws2_32.lib;wsock32.lib;是用来编译Windows网络相关的程序库。
编译,生成!
参考:http://www.cnblogs.com/luxiaoxun/p/3603399.html
linux安装:
- ./configure –prefix=/usr (或 ./configure --program-prefix=/usr)
- make
- make install
注意:
1)执行 make install 时可能需要 root 权限。
我没有设置-prefix。
root@iZ23onhpqvwZ:~/ms/linux/libevent/libevent-2.0.21-stable# whereis libevent
libevent: /usr/local/lib/libevent.a /usr/local/lib/libevent.so /usr/local/lib/libevent.la
运行程序:
root@iZ23onhpqvwZ:~/ms/linux/libevent# gcc -o test1 test1.c -levent
root@iZ23onhpqvwZ:~/ms/linux/libevent# ./test1
./test1: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
解决方法:用ln将需要的so文件链接到/usr/lib或者/lib这两个默认的目录下边
解决办法参考:http://www.cnblogs.com/smartvessel/archive/2011/01/21/1940868.html
http://chenzhou123520.iteye.com/blog/1925196
http://blog.chinaunix.net/uid-20485483-id-82937.html
http://www.open-open.com/lib/view/open1386510630330.html
http://blog.csdn.net/fall221/article/details/9045353
更多:http://book.2cto.com/201404/42124.html
http://www.cnblogs.com/cnspace/archive/2011/07/19/2110891.html
我link时提示警告:
1>LINK : warning LNK4098: 默认库“LIBCMT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
看一篇博客:http://zyan.cc/libevent_windows/
Libcmt.lib
Statically linked library that supports multi-threaded applications.
张老师问你几个问题:ws2_32.lib wsock32.lib 这两个库有什么差别。我用了 ws2_32.lib 这一个库,就能链接成功。
还有,为什么要忽略 libc.lib;msvcrt.lib;libcd.lib;libcmtd.lib;msvcrtd.lib 这些库呢?是什么原因。
我们使用的是静态链接库多线程 (libcmt.lib),这样将一个httpd.exe文件拷贝到别的电脑上也能运行,不需要msvcrt.lib(msvcm80.dll、msvcp80.dll、msvcr80.dl)l等动态链接库的支持。libcmt.lib会和msvcrt.lib发生冲突,需要二者选其一。所以,按照微软MSDN上“链接器工具警告 LNK4098”的说明,忽略了这些库:
http://msdn.microsoft.com/zh-cn/library/6wtdswk0(v=VS.80).aspx
错误消息
默认库“library”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
您试图与不兼容的库链接。
![]() |
---|
运行时库现在包含可防止混合不同类型的指令。如果试图在同一个程序中使用不同类型的运行时库或使用调试和非调试版本的运行时库,则将收到此警告。例如,如果编译一个文件以使用一种运行时库,而编译另一个文件以使用另一种运行时库(例如单线程运行时库对多线程运行时库),并试图链接它们,则将得到此警告。应将所有源文件编译为使用同一个运行时库。有关更多信息,请参见使用运行时库(/MD、/MT 和 /LD)编译器选项。 |
可以使用链接器的 /VERBOSE:LIB 开关来确定链接器搜索的库。如果收到 LNK4098,并想创建使用如单线程、非调试运行时库的可执行文件,请使用/VERBOSE:LIB 选项确定链接器搜索的库。链接器作为搜索的库输出的应是 LIBC.lib,而非 LIBCMT.lib、MSVCRT.lib、LIBCD.lib、LIBCMTD.lib 和 MSVCRTD.lib。对每个要忽略的库可以使用 /NODEFAULTLIB,以通知链接器忽略错误的运行时库。
下表显示根据要使用的运行时库应忽略的库。
若要使用此运行时库 | 请忽略这些库 |
---|---|
单线程 (libc.lib) |
libcmt.lib、msvcrt.lib、libcd.lib、libcmtd.lib、msvcrtd.lib |
多线程 (libcmt.lib) |
libc.lib、msvcrt.lib、libcd.lib、libcmtd.lib、msvcrtd.lib |
使用 DLL 的多线程 (msvcrt.lib) |
libc.lib、libcmt.lib、libcd.lib、libcmtd.lib、msvcrtd.lib |
调试单线程 (libcd.lib) |
libc.lib、libcmt.lib、msvcrt.lib、libcmtd.lib、msvcrtd.lib |
调试多线程 (libcmtd.lib) |
libc.lib、libcmt.lib、msvcrt.lib、libcd.lib、msvcrtd.lib |
使用 DLL 的调试多线程 (msvcrtd.lib) |
libc.lib、libcmt.lib、msvcrt.lib、libcd.lib、libcmtd.lib |
例如,如果收到此警告,并希望创建使用非调试、单线程版本的运行时库的可执行文件,可以将下列选项与链接器一起使用:
- /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:libcd.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib
libevent安装的更多相关文章
- libevent安装总结 - jinfg2008的专栏 - 博客频道 - CSDN.NET
libevent安装总结 - jinfg2008的专栏 - 博客频道 - CSDN.NET libevent安装总结 分类: linux 系统配置 2013-02-13 22:37 99人阅读 评论( ...
- 总结libevent安装方法
1.先用:ls -al /usr/lib | grep libevent 查看是否已安装,如果已安装且版本低于1.3,则先通过:rpm -e libevent -nodeps 进行卸载. 2.下载l ...
- libevent 安装异常
有homebrew的可以使用 1 brew install memcached 这个命令来安装没有homebrew的可以直接手动安装1.去官网http://memcached.org/下载最新的包,然 ...
- libevent安装及使用
一.安装libevent 官网:http://libevent.org/ 选择最新版本下载,我选择的是libevent-2.0.22-stable.tar.gz,然后安装README文件中描述的方法编 ...
- libevent安装后缺少libevent_openssl.so
最近要使用阿里的rocketmq,需要依赖libevent,所以下了个源码自己编译安装,安装过程按照readme来的: 1 ./configure 2 make 3 make install 但是安装 ...
- libevent安装方法
安装FastDFS之前,先安装libevent工具包,记录一下安装过程 1.检查:ls -al /usr/lib | grep libevent 查看是否已安装,如果已安装且版本低于1.3,则先通过: ...
- libevent安装总结
1.先用:ls -al /usr/lib | grep libevent 查看是否已安装:如果已安装且版本低于1.3,则先通过:rpm -e libevent —nodeps进行卸载. 2.下载lib ...
- Linux下Libevent安装和简单实用
前言 Libevent 是一个用C语言编写的.轻量级的开源高性能事件通知库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大:源代码相 ...
- linux下libevent安装
wget http://monkey.org/~provos/libevent-1.4.13-stable.tar.gz tar –xzvf libevent-1.4.13-stable.tar.gz ...
随机推荐
- C# 在运行时动态创建类型
C# 在运行时动态的创建类型,这里是通过动态生成C#源代码,然后通过编译器编译成程序集的方式实现动态创建类型 public static Assembly NewAssembly() { //创建编译 ...
- iterm2 配色修改
打开终端 输入 cd ~ vim .bash_profile 插入 export CLICOLOR= export LSCOLORS=gxfxcxdxbxegedabagacad export PS1 ...
- [译]深入理解JVM
深入理解JVM 原文链接:http://www.cubrid.org/blog/dev-platform/understanding-jvm-internals 每个使用Java的开发者都知道Java ...
- soinn
Growing Cell Structures A Self-Organizing Network for Unsupervised and Supervised Learning Here, and ...
- php5.3不支持 ereg、ereg_replace等函数问题,如提示:Deprecated: Function ereg() is deprecated
在php5.3中,正则函数ereg_replace已经废弃,而dedecms还继续用.有两个方案可以解决以上问题: 1.把php版本换到v5.3下. 2.继续使用v5.3,修改php.ini文件 ;e ...
- Catalyst揭秘 Day4 analyzer解析
Catalyst揭秘 Day4 analyzer解析 今天继续解析catalyst,主要讲一下analyzer,在sql语句的处理流程中,analyzer是在sqlparse的基础上,把unresol ...
- Python urllib2多进程共享cookies
如果想多个进程共享同一个cookies,不用每个进程都重新登录,可以就cookies保存到一个文件,然后多个进程直接共享一个锁来实现 1.一个进程登录完成后,把cookies保存到一个文件里面 sel ...
- 1080. Graduate Admission (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It is said that in 2013, there w ...
- Flashback删除(闪回删除)
oracle 9i以前,当drop一个表时,到该表的所有引用都会从数据字典中删除. oracle 10g及以后,当drop一个表时,数据库根本没有删除表,而只是重命名了并放入了回收站.即当发出drop ...
- Oracle全表扫描
优化器在形成执行计划时需要做的一个重要选择——如何从数据库查询出需要的数据.对于SQL语句存取的任何表中的任何行,可能存在许多存取路径(存取方法),通过它们可以定位和查询出需要的数据.优化器选择其中自 ...