libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。著名分布式缓存软件memcached也是libevent based,而且libevent在使用上可以做到跨平台,而且根据libevent官方网站上公布的数据统计,似乎也有着非凡的性能。

1、下载编译libevent

  下载当前最新的libevent稳定版本libevent-2.0.21-stable.tar.gz安装包,解压到某个固定目录。打开Visual Studio的Command Prompt终端,进入libevent-2.0.21-stable目录,输入如下指令编译libevent代码库:

nmake /f Makefile.nmake

2、收集libevent相关头文件和库文件

  libevent编译完成后,生成的相关静态库文件包括三个:libevent.lib libevent_core.lib libevent_extras.lib,可以单独建立一个lib文件夹存放;

  相关的头文件包括三个地方:(1)libevent-2.0.12-stable\include\*   (2)libevent-2.0.12-stable\WIN32-Code\*    (3)libevent-2.0.12-stable\*.h

把这些头文件都复制一下放到一个单独的include文件夹中方便管理。

3、在VS工程中指定libevent的include和lib文件夹

  有两个地方指定:(1)新建一个工程后,在工程的“配置属性”-->“C/C++”-->“常规”-->“附加包含目录”中给出include文件夹路径;(2)直接在VS“工具”-->“选项”-->“项目和解决方案”-->“VC++目录”中给出include和lib目录路径。

  相比而言,第一种方式我发现在代码中虽然编译不会出错,但是不能自动定位头文件和函数位置;第二种方式就能够清晰定位出libevent头文件和函数的位置。

4、设置工程“运行时库”属性

  在工程的“配置属性”-->“C/C++”-->“代码生成”-->“运行时库”中选择“多线程(/MT)”(这里需要与Libevent库选择相同的编译版本,如果Libevent是Debug版本静态库,则可选择/MD)。

5、设置工程依赖和忽略函数库

  在工程的“配置属性”-->“链接器”-->“输入”中指出“附加依赖项”包括:ws2_32.lib wsock32.lib libevent.lib libevent_core.lib libevent_extras.lib

  这一步的必要性在于,要使用libevent库中的函数,必须要把函数的定义库链接进来。其实,在代码中让编译器连接也是一样的,等价于在代码中添加如下语句:

#include <WinSock2.h>
#include "event.h" #pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libevent.lib")
#pragma comment(lib, "libevent_core.lib")
#pragma comment(lib, "libevent_extras.lib")

6、测试代码

  来一段测试代码试一下:

 #include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <evhttp.h>
void root_handler(struct evhttp_request *req, void *arg)
{
struct evbuffer *buf = evbuffer_new();
if(!buf)
{
puts("failed to create response buffer");
return;
}
evbuffer_add_printf(buf, "Hello: %s\n", evhttp_request_uri(req));
evhttp_send_reply(req, HTTP_OK, "OK", buf);
} void generic_handler(struct evhttp_request *req, void *arg)
{
struct evbuffer *buf = evbuffer_new();
if(!buf)
{
puts("failed to create response buffer");
return;
}
evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));
evhttp_send_reply(req, HTTP_OK, "OK", buf);
} int main(int argc, wchar_t* argv[])
{
struct evhttp *httpd;
WSADATA wsaData;
DWORD Ret;
if ((Ret = WSAStartup(MAKEWORD(, ), &wsaData)) != )
{
printf("WSAStartup failed with error %d\n", Ret);
return -;
}
event_init();
httpd = evhttp_start("0.0.0.0", );
if(!httpd)
{
return ;
}
evhttp_set_cb(httpd, "/", root_handler, NULL);
evhttp_set_gencb(httpd, generic_handler, NULL);
printf("httpd server start OK!\n");
event_dispatch();
evhttp_free(httpd);
WSACleanup();
return ;
}

[原]网络库libevent在Visual Studio中的使用方法的更多相关文章

  1. Visual Studio 中快速创建方法 Generate a method in Visual Studio

    2020-04-04 https://docs.microsoft.com/en-us/visualstudio/ide/reference/generate-method?view=vs-2019 ...

  2. 在Visual Studio 中使用git——浏览版本库(七)

    在Visual Studio 中使用git--什么是Git(一) 在Visual Studio 中使用git--给Visual Studio安装 git插件(二) 在Visual Studio 中使用 ...

  3. 如何在 Visual Studio 中使用 Git 同步代码到 CodePlex

    开源社区不管在国内还是国外都很火热,微软也曾因为没有开源而倍受指责,但是随着 .Net framework.ASP.Net MVC等框架的逐渐开源,也让大家看到了微软开源的步伐.CodePlex 则是 ...

  4. [转]如何在 Visual Studio 中使用 Git 同步代码到 CodePlex

    本文转自:http://www.cnblogs.com/stg609/p/3673782.html 开源社区不管在国内还是国外都很火热,微软也曾因为没有开源而倍受指责,但是随着 .Net framew ...

  5. Visual Studio 中的 .NET Framework 类库

    Visual Studio 中的 .NET Framework 类库 .NET Framework 类库由命名空间组成.每个命名空间都包含可在程序中使用的类型:类.结构.枚举.委托和接口. 当您在 V ...

  6. 在 Visual Studio 中使用 Q# 进行量子编程

    1 量子计算机与量子编程 1.1 量子计算机 Quantum computing is computing using quantum-mechanical phenomena, such as su ...

  7. 利用Visual GDB在Visual Studio中进行Android开发

    转载请注明http://www.cnblogs.com/adong7639/p/4119467.html 无意中发现了Visual GDB这个工具,可以再Visual Studio中进行Android ...

  8. (15)Visual Studio中使用PCL项目加入WCF WebService参考

    原文 Visual Studio中使用PCL项目加入WCF WebService参考 Visual Studio中使用PCL项目加入WCF WebService参考 作者:Steven Chang 2 ...

  9. 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目

    原文: 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目 本主题介绍如何在以下产品中使用 一键式发布 发布(部署)Web 应用程序项目: Visual Studio ...

随机推荐

  1. 转:DataTable.Compute()用法

    转自:http://www.cnblogs.com/fanyf/archive/2012/05/11/2495919.html一.DataTable.Compute()方法說明如下 作用: 计算用来传 ...

  2. (void*)0 的理解

    例如: #define NULL ((void *)0) 用来定义无效的指针 (void *)0 就是将0强制转化为(void *)类型的指针 char *ch = (void *)0;//ch指向地 ...

  3. matio使用

    http://na-wiki.csc.kth.se/mediawiki/index.php/MatIO (1)build根据教程 (2)sudo ldconfig (3)写main根据链接:修改几个类 ...

  4. js实现分页列表添加样式

    <script> var dUrl=window.location.href; var cUrl=(dUrl.substring(0, dUrl.indexOf('list_'))); v ...

  5. CentOS7.0关于libguestfs的bug

    libguestfs,libguestfs-tools是用来在不启动虚拟机的情况下,快速简单访问虚拟机磁盘的工具. 今天在CentOS7.0系统上通过guestmount命令去mount虚拟机磁盘的时 ...

  6. xcode中的一些快捷键

    隐藏xcode command+h退出xcode command+q关闭窗口 command+w关闭所有窗口 command+option+w关闭当前项目 command+control+w关闭当前文 ...

  7. CentOS7上安装和使用Docker

    导读 Docker 是一个开源工具,它可以让创建和管理 Linux 容器变得简单,容器就像是轻量级的虚拟机,并且可以以毫秒级的速度来启动或停止.在本篇文章中我们将教你如何在 CentOS 7.x 中安 ...

  8. 二叉树遍历 空间复杂度为O(1)

    http://blog.csdn.net/mxw976235955/article/details/39829973 http://www.tuicool.com/articles/zA7NJbj / ...

  9. 为什么说Parcelable 比Serializable更高效

    本文转载自:http://blog.csdn.net/androidzhaoxiaogang/article/details/8172539 什么是序列化,实现序列化的目的是什么? 讨论这个问题之前, ...

  10. 【转】iOS10项目打包上传被拒关于隐私权限问题

    原文网址:http://blog.csdn.net/yidu_blog/article/details/53064987 今天项目打包提交.收到了苹果的邮件.主要内容: This app attemp ...