vs2022编译mosquitto.win32版本开启websockets

mosquitto官网windows安装版是支持MQTT协议的,经过测试默认是不支持websocket协议,因此需要下载源码进行编译。

一、需要的工具和库

名称 下载地址 版本
Visual Studio https://visualstudio.microsoft.com/zh-hans/ Community 2022
c-ares https://c-ares.org/ 1.18.1
pthreads ftp://sourceware.org/pub/pthreads-win32 w32-2-9-1-release
openssl http://slproweb.com/products/Win32OpenSSL.html 1.1.1
cmake https://cmake.org/download/ 3.22.1
libwebsockets https://libwebsockets.org/ 4.3
mosquitto https://mosquitto.org/download/ 2.0.14
cJSON https://gitee.com/miao123456miao/cJSON?_from=gitee_search 1.7.15

由于国外网站下载实在太慢,部分源码下载还是选择国内镜像更丝滑。

名称 下载地址 版本
c-ares https://gitee.com/zhang1021/c-ares?_from=gitee_search 1.18.1
libwebsockets https://gitee.com/embedded-lib/libwebsockets?_from=gitee_search 4.3
mosquitto https://gitee.com/pokeryao/mosquitto?_from=gitee_search 2.0.14

二、准备环境

1、将下载好的 pthreads-w32-2-9-1-release.zip 解压到 C盘根目录,重命名为pthreads

2、安装下载好的 Win32OpenSSL-1_1_1L.exe,安装过程一路默认选择下一步。

3、在C盘根目录新建文件夹 application ,在C:\application下新建两个文件夹,分别是source用来放置下载的源码和build用来放置编译后的vs工程。把下载好的libwebsockets、mosquitto、c-ares、cJSON源码统一放在文件夹C:\application\source。

4、安装VS2022,选择”使用C++的桌面开发“

微信截图_20211217212533

三、编译源码

1、编译 libwebsockets

打开cmake-gui,

第一步:Browse Source 选择C:\application\source\libwebsockets

第二步:Browse Build 选择C:\application\build\libwebsockets

第三步:Configure 配置编译环境

第四步:选择编译器版本Visual Studio 17 2022

第五步:选择源码编译版本Win32

第六步:Finish结束配置

第七步:Generate 编译源码

第八步:Open Project 打开VS2022

第九步:在VS2022中选择整个解决方案,右键点击“生成解决方案”或者“重新生成解决方案”编译解决方案。

image-20211217214212912 微信图片_20211217221215

2、编译cJSON,编译步骤和配置与编译libwebsockets相同

image-20211217221939978

微信图片_20211217222008

3、编译mosquitto,编译步骤和配置与编译libwebsockets一样

第一步:勾选WITH_WEBSOCKETS,用于支持websocket

第二步:点击Configure,自动配置之前编译好的websocket

image-20211217224314808image-20211217224519284

第三步:配置cJSON链接库和动态库

第四步:点击Configure进行关联

image-20211217225001983

第五步:点击Generate

第六步:点击Open Project

第七步:

复制C:\pthreads\Pre-built.2\include路径下的所有文件,拷贝到C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include路径下。

复制C:\pthreads\Pre-built.2\lib\x64下的所有文件,拷贝到C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\lib\x64路径下。

复制C:\pthreads\Pre-built.2\lib\x86下的所有文件,拷贝到C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\lib\x86路径下。

复制C:\pthreads\Pre-built.2\dll\x64下的所有文件,拷贝到C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx64\x64路径下。

复制C:\pthreads\Pre-built.2\dll\x64下的所有文件,拷贝到C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx86\x64路径下。

复制C:\pthreads\Pre-built.2\dll\x86下的所有文件,拷贝到C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx64\x86路径下。

复制C:\pthreads\Pre-built.2\dll\x86下的所有文件,拷贝到C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx86\x86路径下。

第八步:选择VS2022解决方案,右键选择“重新生成解决方案”,遇见第一个错误:C2011 “timespec”:“struct”类型重定义 libmosquitto

解决方法:#if !defined( PTHREAD_H )

​ #define PTHREAD_H

后增加:#define HAVE_STRUCT_TIMESPEC

微信截图_20211231101842

第九步:选择VS2022解决方案,右键选择“重新生成解决方案”,遇见第二个编译错误:fatal error C1021: 无效的预处理器命令“warning”。

解决方法:注释掉报错的警告。

image-20211217230641562

第十步:再次选择VS2022解决方案,右键选择“重新生成解决方案”,遇见第三个错误:fatal error C1083: 无法打开包括文件: “cjson/cJSON.h”: No such file or directory。

解决方法:

a、在mosquitto源码的include目录下新建cjson文件夹,完整路径C:\application\source\mosquitto\include\cjson

b、将cJSON源码路径下编译生成的.h文件和.c文件拷贝到刚才新建的cjson文件夹C:\application\source\mosquitto\include\cjson

image-20211217233354621

第十一步:又次选择VS2022解决方案,右键选择“重新生成解决方案”,遇见第四个错误:error C2001: 常量中有换行符

解决方案:替换掉无限符号“∞”为“ ∞ ”

image-20211217235811505

解决完所有错误,重新编译,成功。