news 2026/4/6 23:22:46

Windows11中使用VS2022编译运行libevent网络库

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Windows11中使用VS2022编译运行libevent网络库

Windows11中使用VS2022编译运行libevent事件通知网络库

libevent事件通知库介绍

libevent 是一个异步事件通知软件库。libevent API 提供了一种机制,可以在文件描述符上发生特定事件或超时后执行回调函数。此外,libevent 还支持因信号或常规超时而触发的回调。

下载libevent事件通知库源代码

libevent事件通知库源代码目前托管在Github上,其仓库地址为:https://github.com/libevent/libevent,安装git之后,可以通过如下命令获取libevent源代码:

gitclone https://github.com/libevent/libevent.git

或者

gitclone git@github.com:libevent/libevent.git

也可以直接下载libevent源代码的zip压缩包,如下图所示:

libevent源代码的目录结果如下图所示:

Windows11中使用CMake和VS2022编译运行libevent库

首先我们需要在Windows11上安装CMake和VS2022,这里不再赘述,CMake地址可以去官网https://cmake.org/下载,VS2022可以去微软官网获取并下载安装。

1.参考官方文档编译libevent库

参考https://github.com/libevent/libevent官网的【CMake (Windows)】,如下图所示:

详情可以参考Building on Windows

官方给的使用CMake编译libevent的命令如下:

md build&&cdbuild cmake -G"Visual Studio 10"..# Or use any generator you want to use. Run cmake --help for a listcmake --build.--config Release# Or "start libevent.sln" and build with menu in Visual Studio.

需要注意的是VS2022对应的cmake -G后面的是“Visual Studio 17”,这个可以Win+R打开Windows cmd命令行窗口,输入cmake --help查询到,如下图所示:


Visual Studio 版本
我相信大多数人首先看到的是 Visual Studio 的发布年份,因为 Microsoft 官方就是这么宣传的。例如你可以在官网下载页面看到 Visual Studio 2022、Visual Studio 2019 等等。

但其实 Visual Studio 的版本也有一个更加普遍的 major.minor 版本控制方案,主版本号会在每个发布年份递增。例如 VS 2010 是版本 10,VS 2017 是版本 15,VS 2019 是版本 16,VS 2022 是版本 17。所以,如果客户跟你说:“我用的是 15 版本”,那就意味着它是 Visual Studio 2017。

请注意,Visual Studio 版本的年份和主要版本之间没有任何关联,只是 Visual Studio 2010 恰好也是版本 10。

当然,除了主版本号,Visual Studio 还有次版本号。下表列出了目前主要版本的对应关系。

发布年份实际版本号
Visual Studio 201715.0
15.3
Visual Studio 201916.0
16.1
Visual Studio 202217.0
17.1

所以我们最终在Windows11上使用CMake命令和VS2022安装libevent库的命令为:
我们在解压后的libevent-master源代码打开Windows cmd命令行,依次执行如下命令:

md build&&cdbuild cmake -G"Visual Studio 17"..# Or use any generator you want to use. Run cmake --help for a listcmake --build.--config Release# Or "start libevent.sln" and build with menu in Visual Studio.

上述示例中,“…”指的是包含 Libevent 源代码的目录。您可以通过创建其他构建目录,从同一源代码树构建多个版本(具有不同的编译时设置)。

因此,强烈建议在使用 CMake 时采用“外部构建”(out of source)方式(这样做的好处是不会将源代码目录弄乱,可以针对win32/x64,debug/release组合构建出不同平台不同位数的libevent lib动态或静态库),而不是像 autoconf 的默认行为那样采用“内部构建”(in source)方式。
如果你使用的是VS2015、VS2017、VS2026的话,把17改成对应的数字即可。





执行完上述命令后,会在例如E:\projects\VS2022Porjects\CPlusExamples\MyGithubProjects\libevent-master\build目录下生成libevent.sln这个VS工程文件,以及include、lib库和bin二进制可执行文件等,如下图所示:

2.使用VS2022打开编译后的libevent项目

使用VS2022打开E:\projects\VS2022Porjects\CPlusExamples\MyGithubProjects\libevent-master\build目录下的libevent.sln解决方案文件,如下图所示:

这是一个使用cmake构建的libevent项目,包含需要libevent的示例代码,这里我们先将hello-world设置为启动项目,然后运行,

查看hell-world项目的源代码:

/* This example program provides a trivial server program that listens for TCP connections on port 9995. When they arrive, it writes a short message to each client connection, and closes each connection once it is flushed. Where possible, it exits cleanly in response to a SIGINT (ctrl-c). */#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>staticconstcharMESSAGE[]="Hello, World!\n";staticconstunsignedshortPORT=9995;staticvoidlistener_cb(structevconnlistener*,evutil_socket_t,structsockaddr*,intsocklen,void*);staticvoidconn_writecb(structbufferevent*,void*);staticvoidconn_eventcb(structbufferevent*,short,void*);staticvoidsignal_cb(evutil_socket_t,short,void*);intmain(intargc,char**argv){structevent_base*base;structevconnlistener*listener;structevent*signal_event;structsockaddr_insin={0};#ifdef_WIN32WSADATA wsa_data;WSAStartup(0x0201,&wsa_data);#endifbase=event_base_new();if(!base){fprintf(stderr,"Could not initialize libevent!\n");return1;}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,-1,(structsockaddr*)&sin,sizeof(sin));if(!listener){fprintf(stderr,"Could not create a listener!\n");return1;}signal_event=evsignal_new(base,SIGINT,signal_cb,(void*)base);if(!signal_event||event_add(signal_event,NULL)<0){fprintf(stderr,"Could not create/add a signal event!\n");return1;}event_base_dispatch(base);evconnlistener_free(listener);event_free(signal_event);event_base_free(base);printf("done\n");return0;}staticvoidlistener_cb(structevconnlistener*listener,evutil_socket_t fd,structsockaddr*sa,intsocklen,void*user_data){structevent_base*base=user_data;structbufferevent*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));}staticvoidconn_writecb(structbufferevent*bev,void*user_data){structevbuffer*output=bufferevent_get_output(bev);if(evbuffer_get_length(output)==0){printf("flushed answer\n");bufferevent_free(bev);}}staticvoidconn_eventcb(structbufferevent*bev,shortevents,void*user_data){if(events&BEV_EVENT_EOF){printf("Connection closed.\n");}elseif(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);}staticvoidsignal_cb(evutil_socket_t sig,shortevents,void*user_data){structevent_base*base=user_data;structtimevaldelay={2,0};printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");event_base_loopexit(base,&delay);}

上面代码的作用就是启动一个TCP服务端,本机端口号为9995,有客户端连接时发送"Hello, World!\n"字符串给该客户端,同时可以响应中断信号如Ctrl+C,会打印"Caught an interrupt signal; exiting cleanly in two seconds.\n"字符串,最后会打印done。

先启动完TCP服务端hello-world程序后,使用网络调试助手开启TCP客户端,服务端连接IP地址设置为:127.0.0.1,端口号设置为9995,运行结果如下图所示:

参考资料

  • https://github.com/libevent/libevent
  • Building libevent on Windows
  • https://www.linuxfromscratch.org/blfs/view/svn/basicnet/libevent.html
  • https://github.com/microsoft/vcpkg
    C++ Library Manager for Windows, Linux, and MacOS
  • vcpkg 文档
  • vcpkg 概述
  • https://vcpkg.io/en/
    vcpkg 是一个免费的 C/C++ 包管理器,用于获取和管理库。您可以从 2732 个开源库中进行选择,一步完成下载和构建,或者添加您自己的私有库来简化构建过程。它由 Microsoft C++ 团队和开源贡献者共同维护。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/26 5:20:19

ARM 架构中的数据内存屏障指令 DMB

ARM 架构中的数据内存屏障指令 DMB 本文来自于我关于 ARM架构中内存屏障和同步指令的系列文章。欢迎阅读、点评与交流~ 1、ARM 架构中的数据内存屏障指令 DMB 2、ARM 架构中的数据同步屏障指令 DSB 3、ARM 架构中的指令同步屏障 ISB 核心定义 数据内存屏障指令 DMB 是一种同步…

作者头像 李华
网站建设 2026/4/5 16:49:25

5分钟掌握Transition.css:让你的网页动起来

5分钟掌握Transition.css&#xff1a;让你的网页动起来 【免费下载链接】transition.css :octocat: Drop-in CSS transitions 项目地址: https://gitcode.com/gh_mirrors/tr/transition.css 还在为网页动画效果发愁吗&#xff1f;Transition.css是一个即插即用的CSS过渡…

作者头像 李华
网站建设 2026/4/6 2:28:18

ABC331D Tile Pattern

原题 题目描述 有一个 10^9 乘 10^9的方格网格。让 (i,j)表示从上往下数第 (i1)行、从左往右数第 (j1) 列的方格 每个方格要么是黑色要么是白色。方格 (i,j)的颜色由字符 P[i mod N][j mod N]表示&#xff0c;其中 B 表示黑色&#xff0c;W 表示白色。这里,a mod b表…

作者头像 李华
网站建设 2026/4/1 0:47:26

终极指南:opus-mt-en-zh实战应用全解析

终极指南&#xff1a;opus-mt-en-zh实战应用全解析 【免费下载链接】opus-mt-en-zh 项目地址: https://ai.gitcode.com/hf_mirrors/Helsinki-NLP/opus-mt-en-zh 在当今快速发展的自然语言处理领域&#xff0c;opus-mt-en-zh机器翻译应用以其卓越的性能和广泛适用性&…

作者头像 李华